Java基本的程序结构设计 数组
Posted 张小贱1987
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了Java基本的程序结构设计 数组相关的知识,希望对你有一定的参考价值。
声明数组:
int[] a;
int a[];
两种方式。一般选择第一种,比较一目了然。
初始化,必须指定长度:
int[] a = new int[10];
int[] a = {1,2,3};
初始化:
- package com.zjf;
- import java.util.Arrays;
- import com.zjf.spring.mybatis.model.Person;
- public class Test {
- public static void main(String[] args) {
- //定义数组
- int[] a = new int[3];
- //默认初始化 打印查看为0
- System.out.println(Arrays.toString(a));
- //也可以存储对象
- Person[] p = new Person[2];
- //默认初始化为null
- System.out.println(Arrays.toString(p));
- }
- }
结果:
[0, 0, 0]
[null, null]
数组拷贝
两种方式。
- package com.zjf;
- import java.util.Arrays;
- public class Test {
- public static void main(String[] args) {
- int[] a = {0,1,2,3,4,5,6,7,8,9};
- int[] b = Arrays.copyOf(a, a.length);
- int[] c = new int[a.length];
- System.arraycopy(a, 0, c, 0, a.length);
- System.out.println(Arrays.toString(b));
- System.out.println(Arrays.toString(c));
- }
- }
结果:
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9]
数组排序:
- package com.zjf;
- import java.util.Arrays;
- import com.zjf.spring.mybatis.model.Person;
- public class Test {
- public static void main(String[] args) {
- int[] a = {0,6,2,8,4,5,1,7,3,9};
- Arrays.sort(a);
- System.out.println(Arrays.toString(a));
- Person p1 = new Person(2,"zjf",29,"beiing");
- Person p2 = new Person(1,"xhj",29,"beiing");
- Person[] p = {p1,p2};
- Arrays.sort(p);
- System.out.println(Arrays.toString(p));
- }
- }
结果:
[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)
at com.zjf.Test.main(Test.java:13)
- package com.zjf;
- import java.util.Arrays;
- import java.util.Comparator;
- import com.zjf.spring.mybatis.model.Person;
- public class Test {
- public static void main(String[] args) {
- int[] a = { 0, 6, 2, 8, 4, 5, 1, 7, 3, 9 };
- Arrays.sort(a);
- System.out.println(Arrays.toString(a));
- Person p1 = new Person(2, "zjf", 29, "beiing");
- Person p2 = new Person(1, "xhj", 29, "beiing");
- Person[] p = { p1, p2 };
- Arrays.sort(p, new Comparator<Person>() {
- public int compare(Person o1, Person o2) {
- return o1.getId().compareTo(o2.getId());
- }
- });
- System.out.println(Arrays.toString(p));
- }
- }
结果:
[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 | ||
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 | ||
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 | ||
static String | toString(short[] a) |
以上是关于Java基本的程序结构设计 数组的主要内容,如果未能解决你的问题,请参考以下文章
java小白训练营day03-数据结构:jUnit+基本类型+包装类型+数组+对象