zoukankan      html  css  js  c++  java
  • JAVA先进-设置(1)

    >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内容是否相等;


    版权声明:本文博客原创文章,博客,未经同意,不得转载。

  • 相关阅读:
    Block深入浅出
    JSPatch 遇上swift
    iPhone左下角app图标
    Handoff使用指南
    实习任务——导出excel
    实习任务——对查询结果进行筛选过滤
    Markdown基本语法
    学习笔记(二)——类加载及执行顺序
    #学习笔记(一)——static
    写给过去的3年,拥抱2016
  • 原文地址:https://www.cnblogs.com/lcchuguo/p/4663611.html
Copyright © 2011-2022 走看看