当前位置:网站首页>Use of arrays tool class
Use of arrays tool class
2022-06-25 05:41:00 【Axyzstra】
This class contains various methods for manipulating arrays ( Such as sorting and searching ).
Common methods
- Convert an array to a linked list
public static <T> List<T> asList(T... a)
import java.util.Arrays;
import java.util.List;
public class Test {
public static void main(String[] args) {
String[] str = {
"abc", "def", "ghi", "jkl"};
List<String> list = Arrays.asList(str);
for (String string : list) {
System.out.println(string);
}
}
}
abc
def
ghi
jkl
- Two points search
public static int binarySearch(int[] a,
int key)
// Array types can be replaced with other types
The premise of binary search is that the array must be sorted
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] arr = {
21, 32, 41, 12, 32, 11, 10, -4};
Arrays.sort(arr);
System.out.println(Arrays.binarySearch(arr, 23));
System.out.println(Arrays.binarySearch(arr, 12));
}
}
-6
3
- Copy the array
public static int[] copyOf(int[] original,
int newLength);
public static int[] copyOfRange(int[] original,
int from,
int to)
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] arr = {
21, 33, 41, 12, 32, 11, 10, -4};
int[] copy = Arrays.copyOf(arr, arr.length - 2);
int[] copy1 = Arrays.copyOfRange(arr, 2, 5);
System.out.println("copy");
for (int i : copy) {
System.out.println(i);
}
System.out.println("copy1");
for (int i : copy1) {
System.out.println(i);
}
}
}
copy
21
33
41
12
32
11
copy1
41
12
32
- Determine whether the arrays are equal
public static boolean equals(int[] a,
int[] a2)
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] arr = {
21, 33, 41, 12, 32, 11, 10, -4};
int[] copy = Arrays.copyOf(arr, arr.length);
System.out.println(Arrays.equals(arr, copy));
}
}
true
- Fill in the same values
public static void fill(int[] a,
int val)
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] arr = new int[4];
Arrays.fill(arr, 45);
for (int i : arr) {
System.out.println(i);
}
}
}
45
45
45
45
The method of sorting
Arrays Sorting methods in tool classes sort Generally, it is the default sorting method , Can be a value , String, etc
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] arr = {
21, 33, 41, 12, 32, 11, 10, -4};
Arrays.sort(arr);
for (int i : arr) {
System.out.print(i + " ");
}
}
}
-4 10 11 12 21 32 33 41
Of course , We can also customize the sorting , There are two ways
- Use
ComparatorThe comparator
package aggregate ;
import java.util.Arrays;
import java.util.Comparator;
public class Test {
public static void main(String[] args) {
Stu s1 = new Stu(" Zhang San ", 19);
Stu s2 = new Stu(" Li Si ", 20);
Stu s3 = new Stu(" Wang Wu ", 17);
Stu[] s = new Stu[3];
s[0] = s1;
s[1] = s2;
s[2] = s3;
for (Stu stu : s) {
System.out.println(stu);
}
Arrays.sort(s, new Comparator<Stu>() {
// Sort by age
@Override
public int compare(Stu o1, Stu o2) {
// TODO Auto-generated method stub
return o1.getAge() - o2.getAge();
}
});
System.out.println("--- After ordering -------");
for (Stu stu : s) {
System.out.println(stu);
}
}
}
class Stu {
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Stu(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Stu [name=" + name + ", age=" + age + "]";
}
}
Stu [name= Zhang San , age=19]
Stu [name= Li Si , age=20]
Stu [name= Wang Wu , age=17]
--- After ordering -------
Stu [name= Wang Wu , age=17]
Stu [name= Zhang San , age=19]
Stu [name= Li Si , age=20]
- Array element implementation
ComparableInterface
package aggregate ;
import java.util.Arrays;
import java.util.Comparator;
public class Test {
public static void main(String[] args) {
Stu s1 = new Stu(" Zhang San ", 19);
Stu s2 = new Stu(" Li Si ", 20);
Stu s3 = new Stu(" Wang Wu ", 17);
Stu[] s = new Stu[3];
s[0] = s1;
s[1] = s2;
s[2] = s3;
for (Stu stu : s) {
System.out.println(stu);
}
Arrays.sort(s);
System.out.println("--- After ordering -------");
for (Stu stu : s) {
System.out.println(stu);
}
}
}
class Stu implements Comparable<Stu>{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
public Stu(String name, int age) {
super();
this.name = name;
this.age = age;
}
@Override
public String toString() {
return "Stu [name=" + name + ", age=" + age + "]";
}
@Override
public int compareTo(Stu o) {
return this.getAge() - o.getAge();
}
}
Stu [name= Zhang San , age=19]
Stu [name= Li Si , age=20]
Stu [name= Wang Wu , age=17]
--- After ordering -------
Stu [name= Wang Wu , age=17]
Stu [name= Zhang San , age=19]
Stu [name= Li Si , age=20]
边栏推荐
- Understand JS high-order function and write a high-order function
- Edge loss 解读
- "APEC industry +" biov Tech talks about the cross-border of Chinese biotechnology enterprises and "Pratt & Whitney Kangyu" | apec-hub public welfare
- First blog
- JSON Library Tutorial from scratch (I): starting to learn and organize notes
- Array and simple function encapsulation cases
- Use serialize in egg to read and write split tables
- Tanhaoqiang C language practice
- Notes on non replacement elements in the line (padding, margin, and border)
- Array: force deduction dichotomy
猜你喜欢

Timed thread pool

Stack and Queue

Incorrect dependency of POM file

"APEC industry +" biov Tech talks about the cross-border of Chinese biotechnology enterprises and "Pratt & Whitney Kangyu" | apec-hub public welfare

In depth understanding of line height and vertical align

C language - minesweeping

Interface learning

2.20 learning content

Use of MySQL variables
![[Huawei machine test] hj16 shopping list](/img/54/d28f5aea9350af7797ca7c069e564d.jpg)
[Huawei machine test] hj16 shopping list
随机推荐
For in JS Of and for in
SQL get current time
Detailed summary of position positioning
Jason learning
Click to jump out and drag the pop-up window
Semantic segmentation fcns in the wild: pixel level adaptive and constraint based adaptation
Makefile Foundation
Monkey test of APP automation
ThreadLocal
投资理财产品的年限要如何选?
By inserting a section break, the word header, footer, and page number can start from any page
Edge loss 解读
[pan Wai 1] Huawei computer test
Farewell to Lombok in 996
C language -- Sanzi chess
Interface learning
Trouble of setting table property to null
Personalized Federated Learning with Moreau Envelopes
Detailed summary of flex layout
Using JS to realize the sidebar of life information network