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));
}
}