java中Array(数组)的用法

8.Array(数组)   

数组是作为对象来实现的。(really occupy the memopry,真实的占用内存 )

An array is a data structure that stores a collection of value of the same type.(数组是一个数据结构,它存储一堆类型相同的值)

/*下面这句话只是宣称了一个参考类型的变量,而并没有真正初始化,this statement just declares the reference type variables, not yet initialize*/
int[] a;
string[] b;
char[] c;
/*下面这句话真正初始化了,也就是真正给数组分配了内存,Initialize: Allocate memory for the array.*/
a = new int[10];
b = new String[3];
c = new char[20];

int[] a;
a = new int[3];
a[0] = 1;
a[1] = 2;
a[2] = 3;
System.out.println("the value of a[1] = " + a[1]);
a[1] = 100;
System.out.println("the value of a[1] = " + a[1]);

Initailizer(以下为另一种初始化数组的方法)
int[] a = {1,3,5,7,9};
Array length(数组的长度)
int i=a.length;//5


举例:
int array_int[ ];
String[ ] str;
利用new 来为数组型变量分配内存空间
array_int=new int[10];
str=new String[10];
两步可以合并,如:
String[ ] str=new String[10];

可以在它的length实例变量中找到一个数组的大小——也就是,一个数组能保存的元素的数目 。
所有的数组都有这个变量,并且它总是保存数组的大小。 

8.1 数组的length

Length:数组的容量,而不是数组实际存储的元素的个数(mark, during initialization, 
the value of the array is initialized to 0, if the array 类型 is integer
)
 

// This program demonstrates the length array member.
class Length {
    public static void main(String args[]) {
        int a1[] = new int[10];
        int a2[] = {3,5,6,1,8,45,44,-10};
        int a3[] = {4,3,2,1};
        a1[1]=8;

        System.out.println("length of a1 is " + a1.length);
        System.out.println("length of a2 is " + a2.length);
        System.out.println("length of a3 is " + a3.length);
    }
}

J:\java教程>java Length
length of a1 is 10
length of a2 is 8
length of a3 is 4

更多内容请见原文,原文转载自: https://blog.csdn.net/qq_43650923/article/details/101602752

posted @ 2021-01-04 18:55  师徒行者  阅读(1016)  评论(0编辑  收藏  举报