当前位置:网站首页>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]
边栏推荐
- Excel splits a worksheet into multiple worksheets according to conditions, and how to split multiple worksheets into independent tables
- 投资理财产品的年限要如何选?
- Mirror image of binary tree
- [QT] for multithreaded programs, do not use the printf() function to print out
- JS verification code input number auto skip
- How to choose the years of investment in financial products?
- Trouble of setting table property to null
- Makefile Foundation
- Analysis of IM project framework
- Use serialize in egg to read and write split tables
猜你喜欢

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

Dynamic programming example 1 leetcode 322 coin change

Trouble of setting table property to null

Penetration test - right raising topic

In depth understanding of line height and vertical align

ArcGIS Engine + Visual Studio installation tutorial
![[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

Create an environment for new projects

Use of pytorch tensorboard

hr竟主动给这位测试小姐姐涨工资,她是怎么做到的?
随机推荐
2.20 learning content
Interface learning
电子协会 C语言 1级 28 、字符菱形
SSRF-lab
CopyPlugin Invalid Options options should be array ValidationError: CopyPlugin Invalid Options
Day17 (set)
Day13 (inner class, anonymous inner class, API common class)
CVPR2021-Semi-supervised Domain Adaptation based on Dual-level Domain Mixing for Semantic Segmentati
C style string
Day16 (regular expression, enumeration)
Synchonized introduction
Use of MySQL variables
Day19 (variable parameter, enhanced for loop traversal, generic wildcard <? >, TreeSet, linkedhashset, nested traversal of sets, set set, static import,)
[Huawei machine test] hj16 shopping list
Array introduction plus example 01
2022.1.25
Farewell to Lombok in 996
Go Concurrency
A brief talk on media inquiry
SQL get current time