java数组

数组的定义

type[] 数组名 = new type[数组长度]

int[] arr = {1,1,1,1,2}

数组是一种数据类型,在内存中有栈和堆,栈中数据使用完毕会自动释放,

例如定义 int x= 5, 那么在栈中的main区域便会存储x = 5;

例如int[] x = new int [5] ,在栈中存放x,在堆中存放实体,分配了五个空间,并将第一个空间的地址赋给x,故称为引用类型,堆中 有默认值,0,0.0,0.0f,false;当x = null, 垃圾回收机制

int[] x = new int [3]     int[] y = x;  x=null 堆中有垃圾吗? 没有

 

数组的操作 

遍历

数组长度 arr.length

数组的扩容

  • 循环赋值
  • System.arraycopy(原数组,原数组起始,新数组,新数组起始)
  • java.util.Arrays.copyOf(原数组,新长度)//返回一个新的数组

数组的插入操作

未插入时,数组的初值均为0,故不可以通过遍历来查找插入的位置

寻找一个idx表示数组的最大位置,同时其中涉及到pos的判断以及数组的扩容

public class Insert {
    static int idx;
    static int[] nums = new int[2];
    public static void main(String[] args) {
        insert(0, 11);
        insert(1, 22);
        insert(2, 33);
        insert(3,44);
        print();
    }
    public static void insert(int pos, int val) {
        if(pos < 0 && pos > idx) {//插入位置是否合法
            System.out.println("error");
            return;
        }
        if(idx == nums.length) {
            expend();
        }
        for(int i = idx; i > pos; i --) {
            nums[i] = nums[i - 1];
        }
        nums[pos] = val;
        idx ++;
}
    public static void expend() {
        nums = java.util.Arrays.copyOf(nums, nums.length * 2);
    }
    public static void print() {
        for(int i = 0 ;i < idx; i ++) {
            System.out.print(nums[i]);
            System.out.print(" ");
        }
    }
}

 数组作为函数参数

基本类型的赋值等于值的复制,一方改变,另一方不变

引用类型的复制等于地址的复制,双方同时改变

 

    public static void m(int[] nums){
            nums = java.util.Arrays.copyOf(nums, nums.length * 2);
            nums[0] = 32;
}        

 这段代码并没有改变nums[0]的值,因为扩容的时候改变了地址

public static int[] expend(int[] oldArray){
     int[] newArray = new int [oldarray.length * 2];
     for(int i = 0; i < oldArray.length; i ++){
              newArray[i]  = oldArray[i];
     }
     return newArray;    
}

 可变长参数

概念:可接收多个同类型实参,个数不限,使用同数组

语法:数据类型...形参名  //定义在形参列表的最后,只能有一个

public class ChangeLength {

    public static void main(String[] args) {
        printArray(1,2,3,4,5);
        System.out.println();
        int[] a = {1,2,3,4};
        printArray(a);
        
    }
    public static void printArray(int... array) {
        for(int i = 0; i < array.length; i ++) {
            System.out.print(array[i]);
        }
    }
            
}

12345

1234

本质上就是数组,在源码中常见

数组的排序

java.util.Arrays.sort(nums);

只能进行升序排序

public static int[] mySort(int[] nums){
     for(int i = 0; i < num.length - 1; i ++){
           for(int j = i + 1; j < num.length ; j ++){
               if(nums[i] > nums[j]) swap(nums[i], nums[j]);  
        }
    }  
     return nums;
}    

函数传进来数组的地址,直接进行了交换,可以不用返回数组

二维数组

数组中的元素是数组

int[][] nums = new int [3][5]

nums[i] 本质上是个数组 存放着一个地址

for(int i = 0; i < nums.length; i ++){
   for(int j = 0; j < nums[i].length; j ++){
         System.out.println(nums[i][j]); 
     }  
}

 

申明并赋值

 type[][] nums = new type[高维长度][];

new低维数组: nums[0] = new int[3] //nums[i]长度可以不一样

type[][] nums = {{1,23},{3,4},{2,3}};

 

posted @ 2019-10-09 16:09  lurenjia712  阅读(148)  评论(0)    收藏  举报