数组的拷贝

  • 浅拷贝是指拷贝出来的对象内部的引用类型变量和原来对象内部引用类型变量是同一引用,指向同一对象
    • 但是拷贝出来的对象和新对象不是同一对象。
  • 深拷贝:全部拷贝原对象的内容,包括内存的引用类型也进行拷贝

循环拷贝

用一个for循环进行数组的逐个拷贝

基本类型

int[] array = {1,2,3,4,5,6,7,8,9};
int[] brray = new int[array.length];
for(int i = 0;i < array.length;i++) {
    brray[i] = array[i];
 }
 System.out.println(Arrays.toString(array));  //[1, 2, 3, 4, 5, 6, 7, 8, 9]
 System.out.println(Arrays.toString(brray));  //[1, 2, 3, 4, 5, 6, 7, 8, 9]
 brray[0] = 1000;
 System.out.println("=================");
 System.out.println(Arrays.toString(array));  //[1, 2, 3, 4, 5, 6, 7, 8, 9]
 System.out.println(Arrays.toString(brray)); //[1000, 2, 3, 4, 5, 6, 7, 8, 9]

在这里插入图片描述
引用类型·

class TestArray {
        private int val = 10;
        public void setVal(int val) {
            this.val = val;
        }
        public int getVal() {
            return this.val;
        }
    }


public class Demo3 {
    public static void main(String[] args) {
        TestArray[] t1 = new TestArray[4];
        t1[0] = new TestArray();
        t1[1] = new TestArray();
        t1[2] = new TestArray();
        t1[3] = new TestArray();
        TestArray[] t2 = new TestArray[4];//t2[0]
        for (int i = 0; i < t1.length; i++) {
            t2[i] = t1[i];
        }
        for (int i = 0; i < t1.length; i++) {
            System.out.print(t1[i].getVal() + " ");
        }
        System.out.println();
        for (int i = 0; i < t2.length; i++) {
            System.out.print(t2[i].getVal() + " ");
        }
        System.out.println();
        t2[0].setVal(100000);
        System.out.println("===============");
        for (int i = 0; i < t1.length; i++) {
            System.out.print(t1[i].getVal() + " ");
        }
        System.out.println();
        for (int i = 0; i < t2.length; i++) {
            System.out.print(t2[i].getVal() + " ");
        }

    }
}

在这里插入图片描述

clone方法(浅拷贝)

clone是Object类中一个的方法
基本类型

public static void main4(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8,9}; 
        int[] brray = array.clone();

        System.out.println(Arrays.toString(array));  
        System.out.println(Arrays.toString(brray));
        brray[0] = 1000;
        System.out.println("=================");
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(brray));
    }
    //输出结果:
   // [1, 2, 3, 4, 5, 6, 7, 8, 9]
   //[1, 2, 3, 4, 5, 6, 7, 8, 9]
   //=================
   //[1, 2, 3, 4, 5, 6, 7, 8, 9]
   //[1000, 2, 3, 4, 5, 6, 7, 8, 9]

引用类型(浅拷贝)

  class TestArray {
        private int val = 10;
        public void setVal(int val) {
            this.val = val;
        }
        public int getVal() {
            return this.val;
        }
    }


public class Demo3 {
    public static void main(String[] args) {
        TestArray[] t1 = new TestArray[4];
        t1[0] = new TestArray();
        t1[1] = new TestArray();
        t1[2] = new TestArray();
        t1[3] = new TestArray();
        TestArray[] t2 = t1.clone();//t2[0]
        for(int i = 0;i < t1.length;i++) {
            System.out.print(t1[i].getVal()+" ");
        }
        System.out.println();
        for(int i = 0;i < t2.length;i++) {
            System.out.print(t2[i].getVal()+" ");
        }
        System.out.println();
        t2[0].setVal(100000);
        System.out.println("===============");

        for(int i = 0;i < t1.length;i++) {
            System.out.print(t1[i].getVal()+" ");
        }
        System.out.println();
        for(int i = 0;i < t2.length;i++) {
            System.out.print(t2[i].getVal()+" ");
        }
    }
}
/*10 10 10 10 
10 10 10 10 
===============
100000 10 10 10 
100000 10 10 10 */

System.arraycopy(浅拷贝)

是System类提供的一个静态方法
src和dest必须是同类型或者可以进行转换类型的数组

public static native void arraycopy(Object src,  int  srcPos,
                                                        Object dest, int destPos,
                                                         int length);

src:源数组
srcPos:源数组要复制起始的位置
dest:目的数组
destPos: 目的数组放置的起始位置
length:源数组复制的长度

基本类型

public class Demo3 {
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8,9};
        int[] brray = new int[array.length];
        System.arraycopy(array,0,brray,0,array
                .length);
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(brray));
        brray[0] = 1000;
        System.out.println("=================");
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(brray));
    }
}
//[1, 2, 3, 4, 5, 6, 7, 8, 9]
//[1, 2, 3, 4, 5, 6, 7, 8, 9]
//=================
//[1, 2, 3, 4, 5, 6, 7, 8, 9]
//[1000, 2, 3, 4, 5, 6, 7, 8, 9]

引用类型

package com.tulun.src;

import java.util.Arrays;

class TestArray {
        private int val = 10;
        public void setVal(int val) {
            this.val = val;
        }
        public int getVal() {
            return this.val;
        }
    }


public class Demo3 {
    public static void main(String[] args) {
        TestArray[] t1 = new TestArray[4];
        t1[0] = new TestArray();
        t1[1] = new TestArray();
        t1[2] = new TestArray();
        t1[3] = new TestArray();
        TestArray[] t2 = new TestArray[4];//t2[0]

        System.arraycopy(t1, 0, t2, 0, t1.length);

        for (int i = 0; i < t1.length; i++) {
            System.out.print(t1[i].getVal() + " ");
        }
        System.out.println();
        for (int i = 0; i < t2.length; i++) {
            System.out.print(t2[i].getVal() + " ");
        }
        System.out.println();
        t2[0].setVal(100000);
        System.out.println("===============");

        for (int i = 0; i < t1.length; i++) {
            System.out.print(t1[i].getVal() + " ");
        }
        System.out.println();
        for (int i = 0; i < t2.length; i++) {
            System.out.print(t2[i].getVal() + " ");
        }
    }
}
//        10 10 10 10
//        10 10 10 10
//        ===============
//        100000 10 10 10
//        100000 10 10 10 


Arrays.copyOf()(浅拷贝)

  • Arrays类的方法
  • T[] original(原始数组 ),int newLength(新的数组长度)
  • Arrarys.copyOf底层调用的是System.arraycopy
//源码
 public static <T> T[] copyOf(T[] original, int newLength) {
        return (T[]) copyOf(original, newLength, original.getClass());
    }

基本类型

public class Demo3 {
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6, 7, 8, 9};
        int[] brray = Arrays.copyOf(array, array.length);

        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(brray));
        brray[0] = 1000;
        System.out.println("=================");
        System.out.println(Arrays.toString(array));
        System.out.println(Arrays.toString(brray));
    }
}
//[1, 2, 3, 4, 5, 6, 7, 8, 9]
//[1, 2, 3, 4, 5, 6, 7, 8, 9]
//=================
//[1, 2, 3, 4, 5, 6, 7, 8, 9]
//[1000, 2, 3, 4, 5, 6, 7, 8, 9]

引用类型

import java.util.Arrays;
class TestArray {
        private int val = 10;
        public void setVal(int val) {
            this.val = val;
        }
        public int getVal() {
            return this.val;
        }
    }
public class Demo3 {
    public static void main(String[] args) {
        TestArray[] t1 = new TestArray[4];
        t1[0] = new TestArray();
        t1[1] = new TestArray();
        t1[2] = new TestArray();
        t1[3] = new TestArray();
        TestArray[] t2 = Arrays.copyOf(t1, t1.length);

        for (int i = 0; i < t1.length; i++) {
            System.out.print(t1[i].getVal() + " ");
        }
        System.out.println();
        for (int i = 0; i < t2.length; i++) {
            System.out.print(t2[i].getVal() + " ");
        }
        System.out.println();
        t2[0].setVal(100000);
        System.out.println("===============");

        for (int i = 0; i < t1.length; i++) {
            System.out.print(t1[i].getVal() + " ");
        }
        System.out.println();
        for (int i = 0; i < t2.length; i++) {
            System.out.print(t2[i].getVal() + " ");
        }
    }
}
//10 10 10 10
//10 10 10 10
//===============
//100000 10 10 10
//100000 10 10 10 

将数组中奇数放在偶数前面


public class Demo3 {
    public static void SortNums(int[] array,int low,int high) {
        while(low<high) {
            while(low<high && array[low]%2 != 0 ) {
                low ++;
            }
            while(low<high && array[high]%2 == 0) {
                high --;
            }
            int temp = array[low];
            array[low] = array[high];
            array[high] = temp;
        }
    }
    public static void main(String[] args) {
        int[] array = {1,2,3,4,5,6,7,8,9,10};
        SortNums(nums,0,9);
        for (int i = 0; i < nums.length; i++) {
            System.out.print(nums[i]+"  ");
        }
        System.out.println();
    }
}

//1  9  3  7  5  6  4  8  2  10  

一个数组是有序的,给定一个数字,有两个数字的和加起来等于这个数

public static int[] searchNum(int[] array, int key) {
        int low = 0;
        int high = array.length - 1;
        int sum = 0;
        int[] result = new int[2];
        while (low < high) {
            sum = array[low] + array[high];
            if (sum > key) {
                high--;
            } else if (sum < key) {
                low++;
            } else {
                result[0] = low;
                result[1] = high;
                break;
            }
        }
        return result;
    }

    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6};
        int[] brray = searchNum(array,8);
        System.out.println(Arrays.toString(brray));
    }

排序数组

//源码
public static void sort(int[] a) {
        DualPivotQuicksort.sort(a, 0, a.length - 1, null, 0, 0);
    }
int []array = {3,2,1,5,4};
Arrays.sort(arr);
System.out.println(Arrays.toString(array)); //1,2,3,4,5
//源码
public static void sort(int[] a, int fromIndex, int toIndex) {
        rangeCheck(a.length, fromIndex, toIndex);
        DualPivotQuicksort.sort(a, fromIndex, toIndex - 1, null, 0, 0);
    }

int []array = {3,2,1,5,4};
// 对下标[0, 3)的元素进行排序,即对3,2,1进行排序,5,4保持不变
Arrays.sort(array, 0, 3);
System.out.println(Arrays.toString(array)); //1,2,3,5,4

排序数组并插入某个元素

 public static int[] search(int[] array, int key, int index) {
        int[] array1 = new int[array.length + 1];
        try {
            System.arraycopy(array, 0, array1, 0, index);//1
            array1[index] = key;//1
            System.arraycopy(array, index, array1, index + 1, array.length - index);
        } catch (ArrayIndexOutOfBoundsException e) {//捕获异常
            System.out.println("数组越界异常");
            e.printStackTrace();
        }
        return array1;
    }

    public static void main(String[] args) {
        int[] array = {5, 4, 8, 9, 6};
        Arrays.sort(array);
        try {
            int[] brray = search(array, 10, -8);
            System.out.println(Arrays.toString(brray));
        } catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("数组越界异常");
            e.printStackTrace();
        }
    }
}

搜索数组中的最小值和最大元素

public static int[] maxAndMin(int[] array) {
        int min = array[0];
        int max = array[0];
        int[] result = {0,0};
        for(int i = 0;i < array.length;i++) {
            if(array[i] > max) {
                max = array[i];
            } else if(array[i] < min) {
                min = array[i];
            }
        }
        result[0] = min;
        result[1] = max;
        return result;
    }
    public static void main(String[] args) {
        int[] array = {5, 4, 8, 9, 6};
        int[] result = maxAndMin(array);
        System.out.println(Arrays.toString(result));
    }

合并两个数组

 public static void add(int[] array1,int[] array2,int[] array3){
        System.arraycopy(array1,0,array3,0,array1.length);
        System.arraycopy(array2,0,array3,array1.length,array2.length);
    }


    public static void main(String[] args) {
        int[] array1 = {1,2,3,4,5};
        int[] array2 = {6,7,8,9,10};
        int[] array3 = new int[array1.length + array2.length];
        add(array1,array2,array3);
        System.out.println(Arrays.toString(array3));

    }
}

填充数组

int []arr = new int[5];
Arrays.fill(arr, 2);
//结果是:2 2 2 2 2 
//从起始索引(包括)到结束索引(不包括)
int []arr = new int[5];
Arrays.fill(arr, 0,3,2);
 //2 2 2 0 0 

如何删除数组指定元素

public static int[] delteKey(int[] array,int index) {
        try {
            for(int i = index; i < array.length-1;i++) {
                array[i] = array[i+1];
            }
            array[array.length-1] = -1;
        }catch (ArrayIndexOutOfBoundsException e) {
            System.out.println("数组越界异常");
            e.printStackTrace();
        }
        return array;
    }


    public static void main(String[] args) {
        int[] array = {1,2,3,4,5};
        delteKey(array,3);
        System.out.println(Arrays.toString(array));
    }
    //[1, 2, 3, 5, -1]

从数组中查找常见的元素

public static void main(String[] args) {
       Scanner input = new Scanner(System.in);
        System.out.println("请输入要查找的数组元素:");
        int num = input.nextInt();
        int[] array = {1, 2, 3, 4, 5};
        if (Arrays.binarySearch(array, num) >= 0) {
            System.out.println("该元素的下标为: " + Arrays.binarySearch(array,num));
        } else {
            System.out.println("该元素不存在");
        }
    }

一个整形数组,除了两个数字只出现一次外,其他数字都是两次,找到这两个数字

public static int IndexOfOne(int number) {
            //每次让number右移和1进行与
            int count = 0;
            while ((number & 1) == 0 && count < 32) {
                number = number >> 1;
                count++;
            }
            return count;//返回二进制数中1的个数
        }

        public static boolean isOne(int number, int index) {
            number = number >> index;
            if ((number & 1) == 0) {
                return false;
            }
            return true;
        }

        public static int[] findNumApperOnce(int[] array) {
            //将一维数组里面所有的值进行异或
            int result = 0;
            for (int i = 0; i < array.length; i++) {
                result ^= array[i];
            }
            //从右往左数第index位为2进制1
            int index = IndexOfOne(result);
            int num1 = 0;
            int num2 = 0;
            for (int i = 0; i < array.length; i++) {
                if (isOne(array[i], index)) {
                    num1 ^= array[i];
                } else {
                    num2 ^= array[i];
                }
            }
            int[] resultArr = {0, 0};
            resultArr[0] = num1;
            resultArr[1] = num2;
            return resultArr;
        }

        public static void main(String[] args) {
            int[] array = {1, 2, 3, 3, 2, 5};
            int[] result = findNumApperOnce(array);
            System.out.println(Arrays.toString(result));
        }
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值