当前位置:网站首页>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]
边栏推荐
- Tanhaoqiang C language practice
- Object creation and invocation code example
- Even if you are not good at anything, you are growing a little bit [to your 2021 summary]
- A review of small sample learning
- Read the general components of antd source code
- Double recursion in deep analysis merge sort
- Use js to simply implement the apply, call and bind methods
- Everything is an object
- Mirror image of binary tree
- Depth of binary tree
猜你喜欢
![[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

JMeter stress testing and agent recording

hr竟主动给这位测试小姐姐涨工资,她是怎么做到的?

CVPR2021-Semi-supervised Domain Adaptation based on Dual-level Domain Mixing for Semantic Segmentati

16 application problem solving

Professional things use professional people

Personalized Federated Learning with Moreau Envelopes

Word quickly makes multiple single-sided table labels, number plates, etc

Uva1103 ancient pictograph recognition

Jenkins installation and configuration
随机推荐
05 virtual machine stack
Characteristics of ES6 arrow function
16 application problem solving
Baidu ueeditor set toolbar initial value
H5 native player [learn video]
HR took the initiative to raise the salary of the test lady. How did she do it?
Makefile Foundation
UVA816 Abbott’s Revenge
Click to jump out and drag the pop-up window
Day13 (inner class, anonymous inner class, API common class)
Word of the Day
Unsupervised domain adaptation in semantic segmentation:a review unsupervised domain adaptation in semantic segmentation: a review
How do product managers get started? How do they learn when no one takes them?
Dynamic programming Backpack - 01 Backpack
ArcGIS Engine + Visual Studio installation tutorial
Day18 (set, generic, hash table, tree, stack and queue, graph, array and linked list)
Penetration test - right raising topic
Go Methods and Interfaces
First blog
How to add an external header file in vs?