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

(1)一个人只要自己不放弃自己,整个世界也不会放弃你.
(2)天生我才必有大用
(3)不能忍受学习之苦就一定要忍受生活之苦,这是多么痛苦而深刻的领悟.
(4)做难事必有所得
(5)精神乃真正的刀锋
(6)战胜对手有两次,第一次在内心中.
(7)编写实属不易,若喜欢或者对你有帮助记得点赞+关注或者收藏哦~

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

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

猜你喜欢

转载自blog.csdn.net/xiogjie_67/article/details/108501132