前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >java中Array(数组)的用法

java中Array(数组)的用法

作者头像
马克java社区
修改2021-03-08 10:07:11
6120
修改2021-03-08 10:07:11
举报
文章被收录于专栏:java大数据

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

本文系转载,前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文系转载前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档