您的位置:首页 > 编程语言 > Java开发

java基础知识随笔--数组对象

2015-07-03 20:09 671 查看
1、不管在其他语言中是什么,数组在java中就是对象。

2、定义方法:int[ ] scores = {10,9,9,08,7,6,5,4}; 声明数组时,建议将[ ]放在类型关键词之后。[ ]也可放在声明的名称之后,这是为了让 c/c++开发人员看来比较友好。

3、增强式for循环: int[] sores = {1,2,3,4,5,6,7};
for(int score: sores){
System.out.print(score);
}能输出数组中所有的内容。
4、二维数组的增强式循环: int[][] scores = {{1,2,3},{3,2,1}};
for(int[] rows:scores)
for(int value:rows)
{
System.out.println(value);
}5、
int[] scores1 = {0,0,0,0};
int[] scores2 = scores1;
scores2[0] = 99;
System.out.println(scores1[0]);
输出结果是99,因为数组就是对象。
6、前面说过类的数据成员是会被分配默认值的。因此数组是有默认值的,byte、short、int数组的默认值是0,long数组的默认值是0L,float数组的默认值是0.0F等

7、数组复制的方法: int[] scores1 = {1,2,3,4};
int[] scores2 = Arrays.copyOf(scores1,scores1.length);上面是比较简单的一种方法。

int[] scores1 = {1,2,3,4};
int[] scores2 = scores1;上面并不是复制,只是将scores1参考的数组对象也给scores2参考,因为scores1和scores2是对象,但数组中的值可不是对象。
8、 int i = 10;
int[] scores3 = new int[i];上面不会报错;

int i;
int[] scores3 = new int[i];上面会报错,因为数组是类,需要进行初始化,但是i的值不知道,就不知道初始化几个了。

9、

package cc.openhome;
class Closes {
String nameString;
char sizeChar;
public Closes(String colorString, char size) {
this.nameString = colorString;
this.sizeChar = size;
}
}

package cc.openhome;
public class Field2 {

public static void main(String[] args) {

Closes[] closesArray = {new Closes("red", 's'), new Closes("yellow", 'M')};
Closes[] closesBrray = new Closes[closesArray.length];
System.arraycopy(closesArray, 0, closesBrray, 0, closesArray.length);
System.out.println(closesArray.length);
closesBrray[1].nameString = "white";
System.out.println(closesArray[1].nameString);

}

}
结果输出是white。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: