您的位置:首页 > 编程语言 > Java开发

JAVA进阶-集合(1)

2014-10-16 22:16 169 查看
>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.多维数组的值必须由多个维度共同确定。

变量初始化:当类型数组指定个数时,内存会为数组指定内存 
并给默认的变量,一般根据类型指定,如int指定为0,double为0.0d,String为null;

为方法传递数组以及获取返回值:
当传递参数的时候,系统给方法传递的是数组的引用,因此你可以在数组改变其值.

Copying 和 Cloning 数组:
因为数组在初始化已经固定,当你想为数组扩容的时候,就必须使用复制或者克隆.
系统为Copy提供了一个工具方法,该方法提供源流的目标,开始位置和复制长度;
当数据长度不够复制时抛出ArrayIndexOutOfBoundsException
System.arraycopy(original,0, resultArray,0,original.length);
系统同样提供了拷贝方法array.clone();来实现复制。

在类型数组中,拷贝是复制值,而在对象数组中,只是对对象做了一次浅拷贝;

/**
* @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;
}
}


数组引用和相等的判断:
>当一个变量引用一个数组,比如a引用arrray1,那么把a赋值给变量b,b同样引用array1. 
>当两个对象同时引用一个数组,那么判断他们可以使用a==b;当两个对象通过
克隆复制得到一个数组,那么他们引用不同内存地址,只能使用equals();

数组与字符串:
在C中,总是把char[]数组来表示string,而在Java中,数组跟字符串是可以互转的
string1.toByteArray()把字符串转化成数组;

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));
}
}

}


>The Vector Classes
----Vector----

1.创建Vector可以指定初始容量和增幅,也可以把包装另一个List,如:

Vector v=new Victor(Arrays.asList(array));

2.Vector遵循队列的标准

可以同过add(obj)/addElement()添加到队列末尾,

可以通过索引add(index,obj)/insertElement(obj,index)添加索引所在的位置

也可以将一个Collection队列全部添加到末尾addAll(list);

3.因为该Vector不能添加原始类型,在新版本的jdk中,系统会为我们自动装箱;打印
Vector使用toString();

4.删除Vector可以

通过引用删除remove(obj)/removeElementAt(index);

通过索引删除remove(index)/removeElementAt(index);

通过删除部分removeAll(list)/removeRange(from,to);

全部删除clear()/removeAllElement();

保留Collection所选 删除其他 retainAll(list)

5.替换set(index,obj)/setElement(obj,index)

6.查看/设置队列大小 size()/setSize()

7.存储容量:容量是用来应对动态队列的频繁增减;假使队列容量太小,就应该在增加
队列之前使用ensureCapacity(int minCapacity)先扩容;假设容量不够 一般
增加1倍的容量;与size()不同,size代表当前的个数,容量代表能放多少;当
对队列操作完毕,你可以调用trimToSize()来优化减少空间;

8.Vector的不变性:
假设有一个变量仅做阅读,为防止变化Collections提供了unmodifiableList(v);

9.对元素进行枚举
public Enumeration elements();
while (e.hasMoreElements()) {
process(e.nextElement());
}

扩展AbstractList
Iterator i = v.iterator();
while (i.hasNext()) {
process(e.next());

}

10.多维Vector的迭代取值:
MyType o = (MyType)((Vector)vector.elementAt(3)).elementAt(2);

11.检验Vector元素:
判断存在:contains(obj)/containsAll(list);
索引:indexOf(obj)如果存在2个 得到的是顺序的第1个的索引
反向索引:lastIndexOf(obj)如上反向

12.Copy和clone
调用clone()直接复制或者copyInto(array)先转换成数组再间接复制

13.截取Vector.
public List subList(int fromIndex, int toIndex)

14.使用equals()判断Vector内容是否相等;
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: