java定义数组的三种类型

转载:https://blog.csdn.net/u012110719/article/details/42746817

三种定义数组的格式如下:

int[] arr1=new int[10];  

int[] arr2={1,2,3,6};  

int[] arr3=new int[]{1,2,3,4,5,6,7,22};  

注意:数组的length是一个属性,而字符串的length()是一个方法了!!!虽然都是求的他们各自的长度

package 第四天;  

public class 数组 {  

    public void showArray(int[] arr)  
    {  
        for(int i=0;i<arr.length;i++)  
            System.out.print(arr[i]+"\t");  
        System.out.println();  
    }  
    public static void main(String[] args) {  
        int[] aa;  
//System.out.println(aa);//只是声明了数组没赋初值不能使用会报错误提示),如果new出来了,则系统复int型的值全为0  

        int[] arr1=new int[10];//必须指定大小  

        int[] arr2={1,2,3,6};  
        System.out.println(arr2[0]);  
        int[] arr3=new int[]{1,2,3,4,5,6,7,22};//注意,不能指定大小,这个最常用  

        数组  shuzu=new 数组();  
        shuzu.showArray(arr1);  
        shuzu.showArray(arr2);  
        shuzu.showArray(arr3);  

        shuzu.showArray(new int[]{1,3,2,33});//可以,正确的赋值  
        //shuzu.showArray({2,4,1});//错误,报的是编译错误  
    }  

} 

猜你喜欢

转载自blog.csdn.net/fighting_sxw/article/details/80597909