java数组(二)

数组的常用方法

首先类 Object 是类层次结构的根类。每个类都使用 Object 作为超类。所有对象(包括数组)都实现这个类的方法。 所以java数组继承类Object下的所有方法(以下为常用方法):

boolean equals(Object obj) // 指示其他某个对象是否与此对象“相等”。 

protected  void finalize() //当垃圾回收器确定不存在对该对象的更多引用时,由对象的垃圾回收器调用此方法。 

int hashCode() //返回该对象的哈希码值。 

String toString() //返回该对象的字符串表示。 
数组的复制和拓展

数组的长度一旦定义后,无法改变(元素值可以更改),如果长度变化了,一定是产生了新的数组
,所以出现一些数组复制的方法:

//数组的复制(一)
System.arraryCopy(源数组的名字,源数组的开始下标,目标数组名字,目标数组下标,复制的长度);
//应用
int[] iis = {11, 22, 33};
        //  System:java公司 提供的类名
        // arraycopy:System类 提供的一个功能   作用是实现 数组的复制
        int[] copyIIS = new int[iis.length];
        //  arraycopy(源数组名, 源数组的开始下标,目标数组名,目标数组的下标,复制的长度);
        System.arraycopy(iis, 0, copyIIS, 0, iis.length);
        
        for (int i = 0; i < copyIIS.length; i++) {
            System.out.println(copyIIS[i]);
        }

//数组的复制(二)
Arrarys.copyOf(目标数组名称,目标数组长度或者是扩容长度)
//应用
int[] cs = {100, 200, 300};
        // Arrays:java提供一个类名  记住要导包(java.util.Arrays)
        // copyOf(): 是 Arrays类的功能  作用是:数组复制
        
        int[] newCs = Arrays.copyOf(cs, cs.length);
        System.out.println(Arrays.toString(newCs));
        for (int i = 0; i < newCs.length; i++) {
            System.out.println(newCs[i]);
        }

//数组的复制(三)

	 	 	int[] as = {10, 20, 30};
	        int[] copyAs = new int[as.length];
	        System.out.println("数组复制方法1:");
	        for (int i = 0; i < copyAs.length; i++) {
	            // 将 as数组中i下标位置的元素值 赋值给  copyAs数组的对应 i下标位置
	            copyAs[i] = as[i];
	        }
	
	        // 循环输出 copyAs数组的每一个元素的值
	        for (int i = 0; i < copyAs.length; i++) {
	            System.out.println(copyAs[i]);
	        }

由于数组大小一旦确定就不可更改,所以可以通过数组的复制来实现数组的扩展,如下:

/*例子(1)
* 数组的扩容。(Arrays.copyOf方法)
给定一个数组,要求写一个 expand 函数,把原有数组的长度扩容一倍,并保留原有数
组原有的内容。
* */
public class Expand {
    public static void main(String[] args) {
        int[] e = new int[]{1,2,3,4,5};
        int[] expand = Arrays.copyOf(e,10);
        System.out.println(Arrays.toString(expand));
    }
}
/*例子(2)
*数组的扩容。(System.arraycopy方法)
*/
public class Ex2 {
    public static void main(String[] args) {
        int[] a = {1, 2, 3, 4, 5};
        expand(a);
        System.out.println();
    }
    public static void expand(int[] a) {
        int[] newArray = new int[a.length * 2];
        System.arraycopy(a, 0, newArray, 0, a.length);
    }
}

数组既然可以进行扩展那就可以进行缩容,由此可以实现数组的元素删除和插入:

//删除
public class delete {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 6};
        System.out.println("请输入要删除的数组元素下标");
        Scanner input = new Scanner(System.in);
        //获取用户的输入
        int index = input.nextInt();
        if (index < array.length) {
            for (int i = index; i < array.length - 1; i++) {
                array[i] = array[i + 1];
            }
            System.out.println(Arrays.toString(array));
            int[] newArray = Arrays.copyOf(array, array.length);
            newArray[array.length - 1] = 0;
            System.out.println("删除后的数组为:" + Arrays.toString(newArray));
        } else {
            System.out.println("下标溢出!");
        }
    }
}

//插入
public class insert {
    public static void main(String[] args) {
        int[] ary = {1, 2, 3, 6, 0};
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入要插入元素的下标:");
        int index = scanner.nextInt();
        System.out.println("请输入要插入元素的值:");
        int value = scanner.nextInt();

        if (index < 5) {
            int[] newAry = new int[ary.length];
            for (int i = 0; i < ary.length; i++) {
                newAry[i] = ary[i];
            }
            for (int i = newAry.length - 1; i > index; i--) {
                newAry[i] = newAry[i - 1];
            }
            newAry[index] = value;
            ary = newAry;
            System.out.println(Arrays.toString(ary));
        } else {
            System.out.println("下标溢出!");
        }
    }
}
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值