Java重温学习笔记,关于数组

一、观察下面代码,代码输出包括在里面:

class MyDemo {
    public static void main ( String[] args ) {
        int[] myArray = {1, 2, 3, 4, 5};
        changeIt1(myArray);
        for(int j : myArray)
            System.out.print(j + " " ); // 输出:1 2 3 4 5 

        System.out.println();

        changeIt2(myArray);
        for(int j : myArray)
            System.out.print(j + " " ); // 输出:2 3 4 5 6 
    }
    static void changeIt1(int[] param1) {
        param1 = null ; // 并不改变myArray的引用地址, 也没改变里面的值
    }

    static void changeIt2(int[] param1) {
        for(int j=0; j<param1.length; j++)
            param1[j] = param1[j] +1; // 改变里面的值
    }
}

java 基本数据类型传递参数时是值传递 ;引用类型传递参数时是引用传递 。然而数组虽然是引用传递 ,但是“param1  = null”这条语句,只是将引用param1 不指向任何对象 ,并不会对原先指向的对象数据进行修改 。

二、利用数组做冒泡排序、选择排序

class MyDemo {
    public static void main ( String[] args ) {
       int[] myArray = {26,15,29,66,99,88,36,77,111,1,6,8,8};
       
       System.out.println("当前顺序:");
       for (int i : myArray) {
               System.out.print(i + ",");
       }
       System.out.println();
    
       doBubbleSort(myArray);
        
       System.out.println("冒泡排序后:");
       for (int i : myArray) {
               System.out.print(i + ",");
       }
       System.out.println();

       myArray = new int[]{34,23,56,34,7,89,326,177,6,51,106,88,88};
       
       System.out.println("当前顺序:");
       for (int i : myArray) {
               System.out.print(i + ",");
       }
       System.out.println();
    
       doSelectionSort(myArray);
        
       System.out.println("选择排序后:");
       for (int i : myArray) {
               System.out.print(i + ",");
       }
       System.out.println();
    }
    
    // 冒泡排序
    public static void doBubbleSort(int[] param1) {
        int temp;
        for (int i=0; i<param1.length-1; i++) {
            for (int j=0; j<param1.length-1-i; j++) { // 循环的长度逐渐缩短,因为每次都会有一个大数挪到后面
                if (param1[j] > param1[j+1]) { // 如果前面的大于后面的,则置换;即大数往后挪。注意:此处数组引用是[j+1],所以循环体j变量终止值是param1.length-1-i
                    temp = param1[j];
                    param1[j] = param1[j+1];
                    param1[j+1] = temp;
                }
            }
        }
    }
    
    // 选择排序
    public static void doSelectionSort(int[] param1) {
        int minValuePos;
        for (int i=0; i<param1.length-1; i++) {
            minValuePos= i; // 初始值
            for (int j=i+1; j<param1.length; j++) { 
                // 还有更小值
                if (param1[minValuePos] > param1[j]) { 
                    minValuePos= j;
                }
            }
            
            // 将当前最小值,存放到i的位置
            swapParam(param1, minValuePos, i);
        }
    }
    
    // 交换数组中的两个值,选择排序中调用
    private static void swapParam(int[] param1, int p1, int p2) {
        int temp = param1[p1];
        param1[p1] = param1[p2];
        param1[p2] = temp;
    }
}

 三、java.util.Arrays类能方便地操作数组,它提供的所有方法都是静态的。具有以下功能:

  •  给数组赋值:通过fill方法。
  •  对数组排序:通过sort方法,按升序。
  •  比较数组:通过equals方法比较数组中元素值是否相等。
  •  查找数组元素:通过binarySearch方法能对排序好的数组进行二分查找法操作。
import java.util.Arrays;

public class MyDemo {
    public static void main(String[] args) {
        int[] myArray = new int[5];
        Arrays.fill(myArray, 5); // 数组里面的值全部置为5
        for (int i : myArray) 
            System.out.print(i + " ");  // 5 5 5 5 5 
        System.out.println();
        
        Arrays.fill(myArray, 2, 4, 8); // 数组第2-3位的值置为8(含0)
        for (int i : myArray) 
            System.out.print(i + " "); // 5 5 8 8 5 
        System.out.println();

        int[] myArray2 = { 7, 8, 3, 2, 12, 6, 3, 5, 4 };
        Arrays.sort(myArray2, 2, 7); // 对数组的第2个到第6个进行排序进行排序(含0)
        for (int i : myArray2) 
            System.out.print(i + " "); // 7 8 2 3 3 6 12 5 4 
        System.out.println();
        
        myArray2 = new int[]{17, 28, 13, 62, 12, 96, 3, 15, 48 };
        Arrays.sort(myArray2);
        for (int i : myArray2) 
            System.out.print(i + " "); // 3 12 13 15 17 28 48 62 96 
        System.out.println();

        // 比较数组元素是否相等
        System.out.println(Arrays.equals(myArray, myArray2));  // false
        
        // 查找元素,这是排序后的结果
        System.out.println(Arrays.binarySearch(myArray2, 13));  // 2
    }
}
原文地址:https://www.cnblogs.com/nayitian/p/14901143.html