标签:
>Arrays
基本阵列
1.常见的数组产生于main() 函数,数组下标的索引不能超过0到int的范围
2.当程序试图訪问数组的第一个或者最后一个数据的时候,会发生ArrayIndexOutOfBoundsException异常。
(相当于链表)
3.遗憾的是,数组没有java源码。它是由Object对象组成的带有顺序的集合。
声明/初始化
1.数组的类型有基本类型。Object类型。对于数组的声明仅仅须要添加[]
2.能够是int[] test , int []test , int test[] 三种形式。
3.数组的定义必须声明个数。定义完数组通常使用循环或者一个个赋值。
原始类型/对象类型的数组
原始类型的数组维护着数据的顺序列表,而对象数据则把数据存在对象中,数组仅仅是引用了对象的地址。
多维数组
1.像数组引用对象,如果要维护多个连续的数组 这时候就须要多维数组。
2.多维数组的初始化也要指定长度。比方2维数组,第1个下标表示维护多少个数组,第2个下标表示维护
数组里面的个数。
3.多维数组的值必须由多个维度共同确定。
在类型数组中,拷贝是复制值,而在对象数组中,仅仅是对对象做了一次浅拷贝;
/**
* @author Lean @date:2014-10-16
*/
public class CopyAndCloneArrays {
public static void main(String[] args) throws CloneNotSupportedException {
copyingArray();
}
private static void copyingArray() throws CloneNotSupportedException {
int[] array1={1,2,3,4,5};
int[] array2={1,2,3,4,5,6,7,8,9};
// System.out.println(array1.length+" "+doubleArray(array1).length);
// System.out.println(array2
// .length+" "+doubleArray(array2).length);
Book[] array3={new Book(new Price(20)),new Book(new Price(30))};
Book[] array4=doubleArray(array3);
array3[0].price.value=18;
System.out.println(array3[0].price);
System.out.println(array4[0].price);
}
static int[] doubleArray(int[] original){
int[] resultArray=new int[original.length*2];
System.arraycopy(original,0, resultArray,0,original.length);
return resultArray;
}
static Book[] doubleBookArray(Book[] original){
Book[] resultArray=new Book[original.length*2];
System.arraycopy(original,0, resultArray,0,original.length);
return resultArray;
}
static Book[] doubleArray(Book[] original){
return (Book[])original.clone();
}
}
class Price implements Cloneable{
public int value;
public Price(int value) {
this.value = value;
}
@Override
protected Object clone() throws CloneNotSupportedException {
Price obj=null;
try {
obj=(Price)super.clone();
} catch (Exception e) {
System.out.println(e.getMessage());
}
return obj;
}
@Override
public String toString() {
return value+"";
}
}
class Book implements Cloneable{
public Book(Price price) {
this.price = price;
}
public Price price;
@Override
protected Object clone() throws CloneNotSupportedException {
Book obj=null;
try {
obj=(Book)super.clone();
} catch (Exception e) {
System.out.println(e.getMessage());
}
obj.price=(Price) price.clone();
return obj;
}
}
new String(byte[])能够建立字符串;
数组反射:
/**
* @author Lean @date:2014-10-16
*/
public class ArrayReflection {
public static void main(String[] args) {
// checkArray();
// 定义数组
// reflectNewInstance();
// 存取值
// tryGetSet();
}
private static void tryGetSet() {
int[] data={1,2,3,4};
Array.setInt(data,2, 12);
Array.setInt(data,4, 12);
System.out.println(Array.get(data, 2));
}
private static void reflectNewInstance() {
float[][] bowling=(float[][]) Array.newInstance(float[].class,4);
for (int i = 0; i < bowling.length; i++) {
bowling[i]=(float[]) Array.newInstance(Float.TYPE, i+1);
}
System.out.println(bowling[2].length);
}
private static void checkArray() {
final int[] a={1,2,3};
Class objClass=a.getClass();
if (objClass.isArray()) {
Class componentClass=objClass.getComponentType();
System.out.println(componentClass.getName());
System.out.println(Array.getLength(a));
}
}
}
}
14.使用equals()推断Vector内容是否相等;
版权声明:本文博客原创文章,博客,未经同意,不得转载。
标签:
原文地址:http://www.cnblogs.com/lcchuguo/p/4663611.html