首页 / JAVA / Java基本的程序结构设计 数组
Java基本的程序结构设计 数组
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了Java基本的程序结构设计 数组,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含7941字,纯文字阅读大概需要12分钟。
内容图文
![Java基本的程序结构设计 数组](/upload/InfoBanner/zyjiaocheng/1314/c12742f5bde14ae281ef8416ddc5ed41.jpg)
声明数组:
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) |
原文:http://www.cnblogs.com/xiaolang8762400/p/7041233.html
内容总结
以上是互联网集市为您收集整理的Java基本的程序结构设计 数组全部内容,希望文章能够帮你解决Java基本的程序结构设计 数组所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。