赞
踩
声明的时候并没有实例化对象,只有在实例化数组对象时,JVM才分配空间,这时才与长度有关。
声明数组的时候,数组并没有真正被创建。
类似变量,变量若不初始化,就不能使用。
构造一个数组,需要指定长度。
基本数据类型
public class ArrayTest {
public static void main(String[] args) {
int[] s; //数组声明
s = new int[10]; //数组初始化
for (int i=0;i<10;i++){ //用循环对数组进行存取操作
s[i] = i; //往数组内放值
System.out.println(s[i]); //将值从数组中取出
}
}
}
引用类型
class Person{ //创建person类 private int age; //创建属性 private String name; public Person(int age, String name) { //重写构造方法 this.age = age; this.name = name; } public int getAge() { //数据需要读取,所以创建get、set方法与数组知识点无关 return age; } public void setAge(int age) { this.age = age; } public String getName() { return name; } public void setName(String name) { this.name = name; } } public class ArrayPerson { //主类 public static void main(String[] args) { //main方法 Person[] p = new Person[10]; //声明并初始化数组,数组类型为引用类型 Person p1 = new Person(16,"小王"); //实例化对象 Person p2 = new Person(17,"小张"); p[0] = p1; //将实例化的对象放入数组中 p[1] = p2; System.out.println(p[0].getAge()); //输出:16 System.out.println(p[0].getName()); //输出:小王 } }
int[] c = new int[2]; //默认值:0,0
boolean[] d = new boolean[2]; //默认值:false,false
String[] s = new String[2]; //默认值:null,null
//基本数据类型
int[] a = {1,2,34,5,6,7,8};
//引用类型
Person[] p = {new Person(18,"张三"),new Person(19,"李四")};
//初始化数组并分配空间
int[] b = new int[2];
//给数组元素赋值
b[0] = 1;
//给数组元素赋值
b[1] = 2;
【例:使用for循环遍历数组】
public class ArrayFor { public static void main(String[] args) { int[] a = new int[10]; //使用for循环往数组中存数据 for(int i=0;i<a.length;i++){ a[i] = i; } //使用for循环往数组中读数据 for(int i=0;i<a.length;i++){ System.out.println(a[i]); } } }
输出结果为 :
1
2
3
4
5
...
9
使用for each循环遍历数组
public class ArrayFor { public static void main(String[] args) { int[] a = new int[10]; //使用for循环往数组中存数据 for(int i=0;i<a.length;i++){ a[i] = i; } //int 遍历元素的数据类型 s 变量名 a 需要遍历的数组遍历名 for (int s:a) { System.out.println(s); } } }
输出结果为 :
1
2
3
4
5
...
9
拷贝数组,需要使用System.arraycopy方法。
arraycopy(Object src, int srcPos, Object dest, int destPos, int length);
Object src 原数组 int srcPos 原数组的起始位置
Object dest 目标数组 int destPos 目标数组其实位置
int leng 拷贝长度
**使用System.arraycopy 拷贝数组
public class ArrayCopy {
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6}; //声明并初始化a,b两数组
int[] b = new int[6];
System.arraycopy(a,0,b,0,a.length); //将a的所有信息复制给b
for(int c :b){ //遍历b
System.out.print(c);
}
}
}
System.out.println();是不能直接输出数组元素的。
想要通过System.out.println();输出数组元素,需要使用toString方法。
此处的 Arrays.toString()方法是 Arrays 类的静态方法,不是 Object 的 toString()
方法。
使用 Arrays类下的toString方法输出数组
public class ArrayToString {
public static void main(String[] args) {
int[] a = {1,2,3,4,5,6};
System.out.println(Arrays.toString(a)); //打印数组元素的值
}
}
结果为: [1, 2, 3, 4, 5, 6]
int[] a = {1,4,5,2,3};
System.out.println(Arrays.toString(a)); //排序前
Arrays.sort(a);//对数组进行排序
System.out.println(Arrays.toString(a)); //排序后
输出结果:
[1, 4, 5, 2, 3]
[1, 2, 3, 4, 5]
Arrays 类实现二分法查找法
public class ArrayBinarySearch {
public static void main(String[] args) {
int[] a = {1,2,3,542,234,5,23,423,34};
Arrays.sort(a); //先对数组进行排序
System.out.println(Arrays.binarySearch(a,23));//使用二分法查找,返回值所对应的数组下标。
}
}
输出结果: 4
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。