Java SE 034 Arrays类解析及数组疑难剖析

Java SE 034 Arrays类解析及数组疑难剖析

前言:此笔记为圣思园张龙老师讲述的java视频课程笔记,自己看视频学习时记录的,用于积累与复习,在此分享给学习软件编程的兄弟姐妹们,以供参考。

1.数组只是存放的对象的引用

二维数组里面如果存放的是引用类型的话,数组本身并不存放对象,数组只是存放的是对象的引用。真正的对象是在堆中创建的。数组将多个引用放到一起,然后一起组织。
在这里插入图片描述

2.整数互换位置

public class Swap{
	public static void main(String [] args){
		int a = 3; 
		int b = 4;
		int temp = a ; 
		a = b;
		b = temp;

		System.out.println(a);
		System.out.println(b);

		System.out.println("---------方式二---------");
		int x = 3 ; 
		int y = 4 ; 
		x = x + y;
		y = x - y;
		x = x - y;

		System.out.println(x);
		System.out.println(y);

	}
}

3.数组多态

public class ArrayTest5
{
	public static void main(String [] args){
		I[] i = new I[2];
		I[0] = new C();
		I[1] = new C();

		I[] m = new I[]{new C(),new C()};//这样写也可以。
	}
}

Interface I{

}

class C implements I
{
}

在这里插入图片描述

4.比较两个数组是否一样

public class CompareArray
{
	public static boolean isEquals(int [] a,int [] b){
		if(null == a || null == b){
			return false;
		}
		if(a.length != b.length){
			return false;
		}
		for(int i = 0 ; i < a.length; i ++){
			if(a[i] != b[i]){
				return false;
			}
		}
		return true;
	}


	public static void main(String [] args){
		int [] a = {1,2,3};
		int [] b = {1,2,3};
		System.out.println(isEquals(a,b));
	}
}

方式二:

import java.util.Arrays;
public class CompareArray
{
	public static void main(String [] args){
		int [] a = {1,2,3};
		int [] b = {1,2,3};		
		System.out.println(Arrays.equals(a,b));
	}
}

nt [] a = {1,2,3};
int [] b = {1,2,3};
System.out.println(Arrays.equals(a,b));
}
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值