当前位置:网站首页>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]
边栏推荐
- Go Context - Cancelation and Propagation
- Handwritten promise all
- Interface learning
- Stack and Queue
- Farewell to Lombok in 996
- What happens when you use "-fno exceptions", "new T"- With “-fno-exceptions”, what happens with “new T”?
- Can bus extended frame
- Critical dependency: require function is used in a way in which dependencies
- How do product managers get started? How do they learn when no one takes them?
- Dynamic programming example 2 leetcode62 unique paths
猜你喜欢
![H5 native player [learn video]](/img/51/83a200d0423b7274d1e981ec2ede2c.jpg)
H5 native player [learn video]

Double recursion in deep analysis merge sort

Example of dynamic programming 3 leetcode 55

Stack and Queue

1.6.3 use tcpdump to observe DNS communication process

Common cluster deployment schemes in redis

Notes on non replacement elements in the line (padding, margin, and border)

Farewell to Lombok in 996

Essais de pénétration - sujets d'autorisation
![[day40 literature extensive reading] space and time in the child's mind: metallic or atomic](/img/98/10b3e63c9609990c51b619d9ca6179.jpg)
[day40 literature extensive reading] space and time in the child's mind: metallic or atomic
随机推荐
Common cluster deployment schemes in redis
Install pytorch through pip to solve the problem that torch cannot be used in jupyter notebook (modulenotfoundererror:no module named 'Torch').
Transformations of pytorch torch torch vision
Reading and writing of nodejs excel (.Xlsx) files
2022.1.25
SQL get current time
Trouble of setting table property to null
Use serialize in egg to read and write split tables
Read the general components of antd source code
Conflict between v-mode and v-decorator in Ant Design
2022.1.21 diary
Volatile and JMM memory models
Go Basics
Analysis of IM project framework
Japanese fifty tone diagram
Use of MySQL variables
Jenkins installation and configuration
Day13 (inner class, anonymous inner class, API common class)
ThreadLocal
On Transform