Hướng dẫn - Các cách sắp xếp một mảng Arrays trong java [Sắp xếp trong Java] | VN-Zoom | Cộng đồng Chia Sẻ Kiến Thức Công Nghệ và Phần Mềm Máy Tính

Adblocker detected! Please consider reading this notice.

We've detected that you are using AdBlock Plus or some other adblocking software which is preventing the page from fully loading.

We need money to operate the site, and almost all of it comes from our online advertising.

If possible, please support us by clicking on the advertisements.

Please add vn-z.vn to your ad blocking whitelist or disable your adblocking software.

×

Hướng dẫn Các cách sắp xếp một mảng Arrays trong java [Sắp xếp trong Java]

phule113

Gà con
Sắp xếp trong Java 8

Sắp xếp mảng (Array)
Để sắp xếp các phần tử của mảng, chúng ta sử dụng lớp tiện ích Arrays.sort().
  • Arrays.sort(arr) : Sắp xếp tất cả các phần tử của mảng
  • Arrays.sort(arr, fromIndex, toIndex) : Sắp xếp một phần của mảng
  • Arrays.parallelSort.sort(arr) : Sắp xếp tất cả các phần tử của mảng theo cách xử lý song song. Phương thức này chia nhỏ một mảng thành nhiều mảng con và thực hiện sắp xếp trên các mảng con này một cách song song trên các luồng (Thread) khác nhau, sau đó merge lại để có một mảng được sắp xếp hoàn chình.
  • Arrays.parallelSort.sort(arr, fromIndex, toIndex) : Sắp xếp một phần của mảng theo cách xử lý song song.
ví dụ:
package com.gpcoder.sorting;

import java.util.Arrays;

public class SortedArrayExample {
public static final int NUMBERS[] = { 5, 1, 2, 4, 3, 6, 7, 9, 8 };

public static void main(String[] args) {
// Sorting Complete Array
int arr1[] = Arrays.copyOf(NUMBERS, NUMBERS.length);
Arrays.sort(arr1);
System.out.println(Arrays.toString(arr1));
// => [1, 2, 3, 4, 5, 6, 7, 8, 9]

// Sorting Part of an Array
int arr2[] = Arrays.copyOf(NUMBERS, NUMBERS.length);
Arrays.sort(arr2, 2, 5);
System.out.println(Arrays.toString(arr2));
// => [5, 1, 2, 3, 4, 6, 7, 9, 8]

// Java 8 parallelSort
int arr3[] = Arrays.copyOf(NUMBERS, NUMBERS.length);
Arrays.parallelSort(arr3);
System.out.println(Arrays.toString(arr3));
// => [1, 2, 3, 4, 5, 6, 7, 8, 9]
}
}{lmao}
 


Top