java學習第7天---7.2---數組---數組複製和排序

java學習第7天---7.2---數組---數組複製和排序


目錄




內容

1、數組複製

1.1、遍歷複製

  • 示例代碼1.1-1:

      package array.copy;
    
      public class TestArrayCopy1 {
      	public static void main(String[] args) {
      		int[] arr = { 22, 44, 11, 55};
      		int[] copyArr = new int[4];
      		int i = 0;
      		for(int x: arr) {
      			copyArr[i++] = x;
      		}
    
      		for(int y: copyArr) {
      			System.out.println(y);
      		}
      	}
      }
      測試結果:
      22
      44
      11
      55
    

1.2、System.arraycopy

  • 注意:arraycopy名字不要寫錯

  • 代碼1.2-1:現有數組11, 22, 33, 44,55,66 刪除數組種的33,得到數組11,22,44,55,66

      import java.util.Arrays;
    
      public class TestArray5 {
      	public static void main(String[] args) {
      		// 刪除數組中的33
      		int[] data = {11, 22, 33, 44, 55, 66};
      		int[] arr = new int[5];
      		System.arraycopy(data, 3, data, 2, 3);
      		System.arraycopy(data, 0, arr, 0, 5);
      		for(int x: arr) {
      			System.out.println(x);
      		}
      	}
      }
      測試結果:
      11
      22
      44
      55
      66
    

1.3、Arrays.copyOf

  • 注意:import java.util.Arrays;

  • 示例1.3-1:現有數組11, 22, 33, 44,55,66 刪除數組種的33,得到數組11,22,44,55,66

      import java.util.Arrays;
    
      public class TestArray7 {
      	public static void main(String[] args) {
      		// 刪除數組中的33
      		int[] data = {11, 22, 33, 44, 55, 66};
    
      		System.arraycopy(data, 3, data, 2, 3);
      		int[] arr = Arrays.copyOf(data, 5);
      		for(int x: arr) {
      			System.out.println(x);
      		}
      	}
      }
      測試結果:
      11
      22
      44
      55
      66
    

2、排序

2.1、冒泡排序

2.1.1、原理

  一組數字[23, 44, 77, 243, 99],第一遍排序,數組中的第一個位置23與第二個位置44比較,如果大於44,則交換位置,不大於繼續比較第二個位置和第3個位置的數比較,以此直到數組結尾,那麼第一遍比較最大的數會在末尾。以此進行第二遍比較,直到排序完畢。

1 2 3 4
第一遍 23,44,13,243,99 23,13,44,243,99 23,13,44,243,99 23,13,44,99,243
第二遍 13,23,44,99,243 12,23,44,243 不在交換停止

2.1.2、代碼實現

	升序:
	public class TestArray6 {
		public static void main(String[] args) {
			int[] data = {22, 2, 332, 11, 99};

			a: for(int i = 1; i < data.length; i++) {
				boolean flag = false;
				for(int j = 0; j <= data.length - 1 - i; j++) {
					if(data[j] > data[j+1])	{
						flag = true;
						data[j] ^= data[j+1];
						data[j+1] ^= data[j];
						data[j] ^= data[j+1];
					}
				}
				if(!flag) break a;
			}

			for(int x: data) {
				System.out.println(x);
			}
		}
	}
	測試結果:
	2
	11
	22
	99
	332
	
	降序:
	public class TestArray6 {
	public static void main(String[] args) {
		int[] data = {22, 2, 332, 11, 99};

		a: for(int i = 1; i < data.length; i++) {
			boolean flag = false;
			for(int j = 0; j <= data.length - 1 - i; j++) {
				if(data[j] < data[j+1])	{
					flag = true;
					data[j] ^= data[j+1];
					data[j+1] ^= data[j];
					data[j] ^= data[j+1];
				}
			}
			if(!flag) break a;
		}

		for(int x: data) {
			System.out.println(x);
		}
	}
}

3、小案例

  • 數組:[190, 666, 321, 720, 599],按每個數個位數+十位數+百位數之和降序排序

      package array.sort;
    
      import java.util.Comparator;
      import java.util.Map;
      import java.util.TreeMap;
    
      public class TestArraySort1 {
      	public static void main(String[] args) {
      		// 按照個位數+十位數+百位數的和降序排序
      		int[] data = {190, 666, 321, 720, 599};
      		Comparator<Integer> comparator = new Comparator<Integer>() {
    
      			@Override
      			public int compare(Integer o1, Integer o2) {
      				return o2 - o1;
      			}};
      		Map<Integer, Integer> num = new TreeMap<>(comparator);
    
      		for(int x: data) {
      			int tem = positionSum(x);
    
      			num.put(tem, x);
      		}
    
      		System.out.println(num);
    
    
      	}
    
      	// 求任意整數的個位數+十位數+百位數+...的和
      	public static int positionSum(int n) {
      		if(n / 10 == 0) return n;
      		return (n % 10) + positionSum(n / 10);
      	}
    
      }
      測試結果:
      {23=599, 18=666, 10=190, 9=720, 6=321}
    

2.2、自動排序

  即利用java提供的Arrays.sort進行排序。

  • 示例2.2-1:

      package array.sort;
    
      import java.util.Arrays;
    
      public class TestSort3 {
      	public static void main(String[] args) {
      		int[] data = {110, 126, 90, 99, 135};
      		Arrays.sort(data);
    
      		System.out.println(Arrays.toString(data));
      	}
      }
      測試結果:
      [90, 99, 110, 126, 135]
    
  • 解析

    • Arrays.sort():只適合基本數據類型數組的升序排序
    • sort(T[] a, Comparator<? super T> c):可以自定義排序規則

3、自定義類型的排序

  自定義類型數組排序,需要自定義類型實現Comparable接口或者Comparator接口

示例3-1:
學生類(姓名, 年齡, 分數)
(“小麗”, 22, 77) (“趙剛”, 18, 51) (“王雪” , 20, 95) ("張小強“, 21, 60)

  • 需求1:按學生年齡升序排序

      package array.sort;
    
      import java.util.Arrays;
      import java.util.Comparator;
    
      public class TestStudent2 {
      	public static void main(String[] args) {
      		Student s1 = new Student("趙剛", 18, 51);
      		Student s2 = new Student("王雪", 20, 95);
      		Student s3 = new Student("張小強", 21, 60);
      		Student s4 = new Student("小麗", 22, 77);
    
      		Student[] arr = new Student[4];
      		arr[0] = s4;
      		arr[1] = s2;
      		arr[2] = s3;
      		arr[3] = s1;
    
      		System.out.println(Arrays.toString(arr));
      		Arrays.sort(arr, new Comparator<Student>() {
    
      			@Override
      			public int compare(Student o1, Student o2) {
      				// TODO Auto-generated method stub
      				return o1.getAge() - o2.getAge();
      			}
    
      		});
    
      		System.out.println(Arrays.toString(arr));
    
      	}
      }
      測試結果:
      [Student [name=小麗, age=22, score=77], Student [name=王雪, age=20, score=95], Student [name=張小強, age=21, score=60], Student [name=趙剛, age=18, score=51]]
      [Student [name=趙剛, age=18, score=51], Student [name=王雪, age=20, score=95], Student [name=張小強, age=21, score=60], Student [name=小麗, age=22, score=77]]
    
  • 需求2:按學生的分數降序排序

      package array.sort;
    
      import java.util.Arrays;
      import java.util.Comparator;
    
      public class TestStudent1 {
      	public static void main(String[] args) {
      		Student s1 = new Student("趙剛", 18, 51);
      		Student s2 = new Student("王雪", 20, 95);
      		Student s3 = new Student("張小強", 21, 60);
      		Student s4 = new Student("小麗", 22, 77);
    
      		Student[] arr = new Student[4];
      		arr[0] = s4;
      		arr[1] = s2;
      		arr[2] = s3;
      		arr[3] = s1;
    
      		System.out.println(Arrays.toString(arr));
      		Arrays.sort(arr, new Comparator<Student>() {
    
      			@Override
      			public int compare(Student o1, Student o2) {
      				// TODO Auto-generated method stub
      				return o2.getScore() - o1.getScore();
      			}
    
      		});
    
      		System.out.println(Arrays.toString(arr));
    
      	}
      }
      測試結果:
      [Student [name=小麗, age=22, score=77], Student [name=王雪, age=20, score=95], Student [name=張小強, age=21, score=60], Student [name=趙剛, age=18, score=51]]
      [Student [name=王雪, age=20, score=95], Student [name=小麗, age=22, score=77], Student [name=張小強, age=21, score=60], Student [name=趙剛, age=18, score=51]]
    

後記

本項目爲參考某馬視頻開發,相關視頻及配套資料可自行度娘或者聯繫本人。上面爲自己編寫的開發文檔,持續更新。歡迎交流,本人QQ:806797785

前端項目源代碼地址:https://gitee.com/gaogzhen/vue-leyou
後端JAVA源代碼地址:https://gitee.com/gaogzhen/JAVA
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章