java数组,类,对象

发布于:2022-12-13 ⋅ 阅读:(493) ⋅ 点赞:(0)

1.java数组写法:

int[] arr=new int[3];
表示arr是数组名,int数据类型 ,new为数组申请内存空间,[3]:这个数组共3元素
使用
System.out.println(arr);
arr[0]=100; 把100赋值给它
System.out.println(arr[0]); 打印出这个值

也可以:
int[] arr={1,2,3,4,5}; 直接赋值
使用: arr[0]; arr[4];


public class max {


    public static void main(String[] args){

        int[] arr={1,2,3,4,5};

        int max;
        max=arr[0];

        for(int i=0;i<arr.length;i++)
        {

            if(arr[i]>max){
                max=arr[i];
            }

        }

        System.out.println(max);

    }


}

2.类

:

类:是对现实生活中一类具有共同属性和行为的事物的抽象
包括:类名,成员变量,成员方法:

public class Phone {


    String we="我是说";
int stul;



    public void call(){
        System.out.println("打电话");


    }

3.对象:创建,使用

                       使用另一个类中的东西,

写法:
类名 对象名 =new 类名(); 对象名可自己修改
如: Phone p=new Phone();
使用对象:
使用对象的成员 对象名.变量名 p.stul
使用对象的方法 对象名.方法名() p.call();

public class Phonemoe {


    public static void main(String[] args){



        Phone p=new Phone();



System.out.println(p.stul);
        System.out.println(p.we);
        p.call();
    }

9.18号补充:
数组:打印一个用 a[n];n第几个元素
打印全部?
System.out.println(a); //打印的是首地址
方法一:借助Array (import java.util.Arrays)来转化

import java.util.Arrays;

public class shuzu {


    public static void main(String[] args) {


        int[] a={1,2,3,4,5,6,7};

        System.out.println(a);
        System.out.println("转化"+Arrays.toString(a));
        
    }
    
}

结果:
在这里插入图片描述

本文含有隐藏内容,请 开通VIP 后查看

网站公告

今日签到

点亮在社区的每一天
去签到