标签:cep ide param ble dcom bool retain war obj
package cn.zhuj.ArrayList1; import java.util.ArrayList; import java.util.Arrays; import java.util.Iterator; import java.util.ListIterator; import cn.zhuj.util.ArrayListCapacity; import cn.zhuj.util.outPut; public class ArraylistTest { public static void main(String args[]) { // create a Dog Arraylist use the initialCapacity of 20 ArrayList<Dog> arraylist = new ArrayList<Dog>(); ArrayList<Dog> arraylist1 = new ArrayList<Dog>(5); arraylist.add(new Dog("pony", 45)); arraylist.add(new Dog("lucy", 15)); arraylist.add(new Dog("john", 25)); arraylist.add(new Dog("mino", 35)); arraylist.add(new Dog("tony", 45)); arraylist.add(new Dog("JJ", 16)); arraylist.add(new Dog("jackie", 55)); arraylist.add(new Dog("nono", 18)); arraylist.add(new Dog("JJ", 16)); arraylist1.add(new Dog("jackie", 55)); arraylist1.add(new Dog("bruce", 65)); arraylist1.add(new Dog("tony", 25)); arraylist1.add(new Dog("pony", 45)); //add the element to the specified index of the ArrayList arraylist.add(1, new Dog("bruce", 65)); //append all the elements of the specified ArrayList to the end of this ArrayList // arraylist.addAll(arraylist1); //append all the elements of the specified ArrayList to the specified index of this ArrayList // arraylist.addAll(2, arraylist1); //remove all the elements of the specified ArrayList //arraylist.clear(); // if this ArrayList contains the specified element outPut.outPut("=======contains(Object o)=======",0); outPut.outPut("arraylist.contains = "+arraylist.contains(new Dog("JJ",16)),0); //if this ArrayList contains the specified ArrayList outPut.outPut("=======containsAll(Collection<? extends E> c)=======",0); outPut.outPut("arraylist.contrainsAll = "+arraylist.containsAll(arraylist1),0); //expand this ArrayList‘s capacity must over 1.5 times of the oldCapacity otherwise capacity will be 1.5*oldCapacity instead of the argument arraylist.ensureCapacity(16); //java 1.8 new Lambda expression like javaScript ES6 outPut.outPut("=======forEach(Consumer<? super E>action)========",0); arraylist.forEach(e->outPut.outPut(e)); //cout/syso the specified index element of this ArrayList outPut.outPut("\n=======get(int index)=======",0); outPut.outPut("index(1) = ",arraylist.get(1));; Dog d = new Dog("JJ",16); // the index of the first occurrence of the specified element of the ArrayList outPut.outPut("\n=======indexOf(Object o)=======",0); outPut.outPut("index = "+arraylist.indexOf(d),0); // the index of the last occurrence of the specified element of the ArrayList outPut.outPut("=======lastIndexOf(Object o)=======",0); outPut.outPut("lastIndex = "+arraylist.lastIndexOf(d),0); // return proper sequence called iterator outPut.outPut("=======iterator()=======",0); Iterator<Dog> it = arraylist.iterator(); while(it.hasNext()) { Dog d2 = it.next(); if(d2.getName() == "mino") { outPut.outPut("mino‘s age = "+d2.getAge(),0); } } //this ArrayList contains no element outPut.outPut("=======isEmpty()========",0); outPut.outPut("isEmpty = "+arraylist.isEmpty(),0); //use reflection to show the capacity of this ArrayList outPut.outPut("=======java.lang.reflect========",0); ArrayListCapacity.ArrayListCapacity(arraylist); // return proper sequence called listIterator outPut.outPut("=======ListIterator()========",0); ListIterator<Dog> lit = (ListIterator<Dog>) arraylist.listIterator(); while(lit.hasNext()) { Dog dog = lit.next(); if(dog.getName() == "nono") { outPut.outPut("nono‘s age = "+dog.getAge(),0); } } // return proper sequence called listIterator,strat at the specified index outPut.outPut("=======ListIterator(int index)========",0); ListIterator<Dog> lit2 = (ListIterator<Dog>) arraylist.listIterator(3); while(lit2.hasNext()) { Dog dog = lit2.next(); outPut.outPut(dog.getName(),1); } // use Arrays.copyOf() to copy this ArrayList belongs to shallow copying outPut.outPut("\n=======clone()=======",1); @SuppressWarnings("unchecked") ArrayList<Dog> array = (ArrayList<Dog>)arraylist.clone(); // remove a element of this ArrayList array.remove(1); // remove "john" element of this ArrayList array.remove(new Dog("john",21)); // remove the range elements of this ArrayList array.removeRange() not open to user array.subList(1, 3).clear(); outPut.outPut("", 0); ListIterator<Dog> lit1 = (ListIterator<Dog>) array.listIterator(); while(lit1.hasNext()) { Dog dog = lit1.next(); outPut.outPut(dog.getName(),1); } // retain the union between arraylist and arraylist1 outPut.outPut("", 0); outPut.outPut("=======retainAll(Collection<?> c)=======", 0); arraylist1.retainAll(arraylist); arraylist1.forEach(e->outPut.outPut(e)); // arraylist remove arraylist1 remove the union between arraylist and arraylist1 outPut.outPut("", 0); outPut.outPut("=======removeAll(Collection<?> c)+removeIf(Predicate<? super E> filter) =======", 0); arraylist.removeAll(arraylist1); arraylist.removeIf(s -> s.getAge()%5==0); arraylist.replaceAll(s->{ s.setName(s.getName().toUpperCase()); return s; }); arraylist.forEach(e->outPut.outPut(e)); // replace the element at the specified index of this ArrayList outPut.outPut("", 0); outPut.outPut("======= set(int index,E element) =======", 0); arraylist.set(0, new Dog("goudan",22)); arraylist.forEach(s->outPut.outPut(s)); //arraylist real length outPut.outPut("", 0); outPut.outPut("======= size() =======", 0); outPut.outPut(arraylist.size(),0); // sort this ArrayList by XXComparator outPut.outPut("======= sort(Comparator<? extends E> c) =======", 0); DogComparator dcomparator = new DogComparator(); arraylist.sort(dcomparator); arraylist.forEach(s->outPut.outPut(s,0)); //TODO spliterator() // toArray() outPut.outPut("======= toArray() =======", 0); Object a [] = arraylist.toArray(); outPut.outPut(Arrays.toString(a), 0); // toArray(T[] a) outPut.outPut("======= toArray(T[] a) =======", 0); Dog [] dogArr = new Dog[arraylist.size()]; arraylist.toArray(dogArr); outPut.outPut(Arrays.toString(dogArr), 0); //condense this ArrayList‘s capacity to real size. outPut.outPut("======= trimToSize() =======", 0); arraylist.trimToSize(); ArrayListCapacity.ArrayListCapacity(arraylist); } }
package cn.zhuj.util; import java.lang.reflect.Field; import java.util.ArrayList; public class ArrayListCapacity { public static void ArrayListCapacity(ArrayList<?> arraylist) { Class<? extends Object> cls = ((Object)arraylist).getClass(); Field field; try { field = cls.getDeclaredField("elementData"); field.setAccessible(true); try { Object[] o = (Object[]) field.get(arraylist); outPut.outPut("object[]‘s length is "+o.length,0); } catch (IllegalAccessException e) { // TODO Auto-generated catch block e.printStackTrace(); } } catch (NoSuchFieldException e) { // TODO Auto-generated catch block e.printStackTrace(); } catch (SecurityException e) { // TODO Auto-generated catch block e.printStackTrace(); } } } package cn.zhuj.util; import cn.zhuj.ArrayList1.Dog; public class outPut { public static void outPut(Dog d) { // TODO Auto-generated method stub System.out.print(d.getName() + "\t"); } public static void outPut(Object e ,int i) { // TODO Auto-generated method stub if (i == 0) System.out.println(e); else System.out.print(e+"\t"); } public static void outPut(String s,Dog d) { System.out.print(s+d.getName() + "\t"); } } package cn.zhuj.ArrayList1; import java.util.Comparator; public class DogComparator implements Comparator<Dog>{ /* (non-Javadoc) * @see java.util.Comparator#compare(java.lang.Object, java.lang.Object) */ @Override public int compare(Dog o1, Dog o2) { // TODO Auto-generated method stub return o1.getAge()-o2.getAge(); } } package cn.zhuj.ArrayList1; public class Dog { private String name; private int age; /** * @return the name */ public String getName() { return name; } /** * @param name the name to set */ public void setName(String name) { this.name = name; } /** * @return the age */ public int getAge() { return age; } /** * @param age the age to set */ public void setAge(int age) { this.age = age; } /* (non-Javadoc) * @see java.lang.Object#toString() */ @Override public String toString() { // TODO Auto-generated method stub return this.name+"-"+this.age; } /* (non-Javadoc) * @see java.lang.Object#hashCode() */ @Override public int hashCode() { final int prime = 31; int result = 1; result = prime * result + age; result = prime * result + ((name == null) ? 0 : name.hashCode()); return result; } /* (non-Javadoc) * @see java.lang.Object#equals(java.lang.Object) */ @Override public boolean equals(Object obj) { if (this == obj) return true; if (obj == null) return false; if (getClass() != obj.getClass()) return false; Dog other = (Dog) obj; if (age != other.age) return false; if (name == null) { if (other.name != null) return false; } else if (!name.equals(other.name)) return false; return true; } public Dog(String name, int age) { super(); this.name = name; this.age = age; } public Dog() { super(); // TODO Auto-generated constructor stub } }
标签:cep ide param ble dcom bool retain war obj
原文地址:https://www.cnblogs.com/zhju/p/8796639.html