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

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。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值