声明数组:
int[] a;
int a[];
两种方式。一般选择第一种,比较一目了然。
初始化,必须指定长度:
int[] a = new int[10];
int[] a = {1,2,3};
初始化:
1. package com.zjf;
2.
3. import java.util.Arrays;
4.
5. import com.zjf.spring.mybatis.model.Person;
6.
7. public class Test {
8.
9. public static void main(String[] args) {
10. //定义数组
11. int[] a = new int[3];
12. //默认初始化 打印查看为0
13. System.out.println(Arrays.toString(a));
14. //也可以存储对象
15. new Person[2];
16. //默认初始化为null
17. System.out.println(Arrays.toString(p));
18.
19. }
20. }
结果:
[0, 0, 0]
[null, null]
数组拷贝
两种方式。
1. package com.zjf;
2. import java.util.Arrays;
3. public class Test {
4. public static void main(String[] args) {
5. int[] a = {0,1,2,3,4,5,6,7,8,9};
6. int[] b = Arrays.copyOf(a, a.length);
7. int[] c = new int[a.length];
8. System.arraycopy(a, 0, c, 0, a.length);
9. System.out.println(Arrays.toString(b));
10. System.out.println(Arrays.toString(c));
11. }
12. }
结果:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
数组排序:
1. package com.zjf;
2. import java.util.Arrays;
3. import com.zjf.spring.mybatis.model.Person;
4. public class Test {
5. public static void main(String[] args) {
6. int[] a = {0,6,2,8,4,5,1,7,3,9};
7. Arrays.sort(a);
8. System.out.println(Arrays.toString(a));
9. new Person(2,"zjf",29,"beiing");
10. new Person(1,"xhj",29,"beiing");
11. Person[] p = {p1,p2};
12. Arrays.sort(p);
13. System.out.println(Arrays.toString(p));
14. }
15. }
结果:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
Exception in thread "main" java.lang.ClassCastException: com.zjf.spring.mybatis.model.Person cannot be cast to java.lang.Comparable
at java.util.ComparableTimSort.countRunAndMakeAscending(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.ComparableTimSort.sort(Unknown Source)
at java.util.Arrays.sort(Unknown Source)
Test.java:13)
1. package com.zjf;
2.
3. import java.util.Arrays;
4. import java.util.Comparator;
5.
6. import com.zjf.spring.mybatis.model.Person;
7.
8. public class Test {
9. public static void main(String[] args) {
10. int[] a = { 0, 6, 2, 8, 4, 5, 1, 7, 3, 9 };
11. Arrays.sort(a);
12. System.out.println(Arrays.toString(a));
13. new Person(2, "zjf", 29, "beiing");
14. new Person(1, "xhj", 29, "beiing");
15. Person[] p = { p1, p2 };
16. new Comparator<Person>() {
17. public int compare(Person o1, Person o2) {
18. return o1.getId().compareTo(o2.getId());
19. }
20. });
21. System.out.println(Arrays.toString(p));
22. }
23. }
结果:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[id:1,name:xhj, id:2,name:zjf]
注:Arrays.sort使用的是快速排序算法。
Arrays的API:
方法摘要 | |
static | asList(T... a) |
static int | binarySearch(byte[] a, byte key) |
static int | binarySearch(char[] a, char key) |
static int | binarySearch(double[] a, double key) |
static int | binarySearch(float[] a, float key) |
static int | binarySearch(int[] a, int key) |
static int | binarySearch(long[] a, long key) |
static int | binarySearch(Object[] a, Object key) |
static int | binarySearch(short[] a, short key) |
static | binarySearch(T[] a, T key, Comparator<? super T> c) |
static boolean | deepEquals(Object[] a1, Object[] a2) |
static int | deepHashCode(Object[] a) |
static String | deepToString(Object[] a) |
static boolean | equals(boolean[] a, boolean[] a2) |
static boolean | equals(byte[] a, byte[] a2) |
static boolean | equals(char[] a, char[] a2) |
static boolean | equals(double[] a, double[] a2) |
static boolean | equals(float[] a, float[] a2) |
static boolean | equals(int[] a, int[] a2) |
static boolean | equals(long[] a, long[] a2) |
static boolean | equals(Object[] a, Object[] a2) |
static boolean | equals(short[] a, short[] a2) |
static void | fill(boolean[] a, boolean val) |
static void | fill(boolean[] a, int fromIndex, int toIndex, boolean val) |
static void | fill(byte[] a, byte val) |
static void | fill(byte[] a, int fromIndex, int toIndex, byte val) |
static void | fill(char[] a, char val) |
static void | fill(char[] a, int fromIndex, int toIndex, char val) |
static void | fill(double[] a, double val) |
static void | fill(double[] a, int fromIndex, int toIndex, double val) |
static void | fill(float[] a, float val) |
static void | fill(float[] a, int fromIndex, int toIndex, float val) |
static void | fill(int[] a, int val) |
static void | fill(int[] a, int fromIndex, int toIndex, int val) |
static void | fill(long[] a, int fromIndex, int toIndex, long val) |
static void | fill(long[] a, long val) |
static void | fill(Object[] a, int fromIndex, int toIndex, Object val) |
static void | fill(Object[] a, Object val) |
static void | fill(short[] a, int fromIndex, int toIndex, short val) |
static void | fill(short[] a, short val) |
static int | hashCode(boolean[] a) |
static int | hashCode(byte[] a) |
static int | hashCode(char[] a) |
static int | hashCode(double[] a) |
static int | hashCode(float[] a) |
static int | hashCode(int[] a) |
static int | hashCode(long[] a) |
static int | hashCode(Object[] a) |
static int | hashCode(short[] a) |
static void | sort(byte[] a) |
static void | sort(byte[] a, int fromIndex, int toIndex) |
static void | sort(char[] a) |
static void | sort(char[] a, int fromIndex, int toIndex) |
static void | sort(double[] a) |
static void | sort(double[] a, int fromIndex, int toIndex) |
static void | sort(float[] a) |
static void | sort(float[] a, int fromIndex, int toIndex) |
static void | sort(int[] a) |
static void | sort(int[] a, int fromIndex, int toIndex) |
static void | sort(long[] a) |
static void | sort(long[] a, int fromIndex, int toIndex) |
static void | sort(Object[] a) |
static void | sort(Object[] a, int fromIndex, int toIndex) |
static void | sort(short[] a) |
static void | sort(short[] a, int fromIndex, int toIndex) |
static | sort(T[] a, Comparator<? super T> c) |
static | sort(T[] a, int fromIndex, int toIndex, Comparator<? super T> c) |
static String | toString(boolean[] a) |
static String | toString(byte[] a) |
static String | toString(char[] a) |
static String | toString(double[] a) |
static String | toString(float[] a) |
static String | toString(int[] a) |
static String | toString(long[] a) |
static String | toString(Object[] a) |
static String | toString(short[] a) |
<T> List<T> |