@Test java
public void test1() {數組
String[] str = new String[] { "Jack", "Alex", "Ami", "Jerry" };ide
Arrays.sort(str);this
// 顯示已經排序的數據spa
System.err.println(Arrays.toString(str));.net
}對象
@Test 排序
public void test1() {接口
String[] str = new String[] { "Jack", "Alex", "Ami", "Jerry" };內存
Arrays.sort(str, 1, 3);
// 顯示已經排序的數據
System.err.println(Arrays.toString(str));
}
在sort裏面,若是想實現對對象排序,你的類必需要實現:
兩種:
1:實現接口comparable
2:提供一個比較器。
public class Demo03 {
public void test1() {
// 默認全部系統的類Int,Integer,double ,float ,String能夠排序
Stud[] studs = new Stud[] { new Stud("Jerry"), new Stud("Alex"), new Stud("Jack") };
Arrays.sort(studs);
System.err.println(Arrays.toString(studs));
}
class Stud implements Comparable<Stud> {
private String name;
public Stud(String name) {
this.name = name;
}
@Override
public String toString() {
return "Stud [name=" + name + "]";
}
@Override
public int compareTo(Stud o) {
return o.name.compareTo(this.name);
}
}
}
package cn.demo;
import java.lang.reflect.Array;
import java.util.Arrays;
import java.util.Comparator;
import org.junit.Test;
public class Demo03 {
@Test
public void test1() {
// 默認全部系統的類Int,Integer,double ,float ,String能夠排序
Stud[] studs = new Stud[] { new Stud("Jerry"), new Stud("Alex"), new Stud("Jack") };
Arrays.sort(studs, new Comparator<Stud>() {
@Override
public int compare(Stud o1, Stud o2) {
return o2.getName().compareTo(o1.getName());
}
});
System.err.println(Arrays.toString(studs));
}
}
class Stud {
private String name;
public Stud(String name) {
this.name = name;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Stud [name=" + name + "]";
}
}
class Stud {
private String name;
private int age;
public Stud(String name, int age) {
this.name = name;
this.age = age;
}
public String getName() {
return name;
}
@Override
public String toString() {
return "Stud [name=" + name + ", age=" + age + "]";
}
}
public class Demo04 {
@Test
public void test1(){
int[] a = {22,33,445};
Arrays.fill(a, 100);//所有設置成100
System.err.println(Arrays.toString(a));
}
}
[100, 100, 100]
@Test
public void test1() {
int[] a1 = { 22, 33, 445 };// new Int[]{}...
int[] a2 = { 22, 33, 445 };// new int[]{}
System.err.println(a1 == a2);// false
System.err.println(a1.equals(a2));// false; 內存地址不相等
boolean boo = Arrays.equals(a1, a2);
System.err.println(boo);// true
}
@Test
public void test1() {
int[] a1 = { 22, 33, 445 };// new Int[]{}...
int[] a2 = Arrays.copyOf(a1, 5);//數組增加
System.err.println(Arrays.toString(a2));
}
public class Demo04 {
@Test
public void test1() {
int[] a1 = { 22, 33, 445 };// new Int[]{}...
int[] a2 = Arrays.copyOfRange(a1, 1, 3);// >=from and <to
System.err.println(Arrays.toString(a2));
}
}
關於二分法查找 本身能夠開發出來:
原則:只能查詢已經排過序的數組。
public class Demo04 {
@Test
public void test1() {
int[] a = { 1, 2, 3, 4, 5, 6 };
int num = -5;
int left = 0;
int right = a.length - 1;
int idx = -1;
while (left <= right) {
int mid = (left + right) / 2;
int no = a[mid];
if (num > no) {
left = mid + 1;
} else if (num < no) {
right = mid - 1;
} else {
idx = mid;
break;
}
}
System.err.println("index is:" + idx);
}
}
@Test
public void test2() {
int[] a = {5,6,7,78,54,3,3,3 , 2, 4, 6, 8 };
Arrays.sort(a);
int idx = Arrays.binarySearch(a, 7);
System.err.println(idx);
}