Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
93 changes: 93 additions & 0 deletions src/btp/oneP/ApplyTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
package btp.oneP;

import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.LinkedList;
import java.util.List;

public class ApplyTest {

public static void main(String[] args) throws NoSuchMethodException, SecurityException {
List<Shape> shapes = new ArrayList<Shape>();
for(int i=0;i<10;i++){
shapes.add(new Shape());
}
Apply.apply(shapes, Shape.class.getMethod("rotate"));

Apply.apply(shapes, Shape.class.getMethod("resize", int.class), 5);


List<Square> squares = new ArrayList<Square>();
for(int i=0;i<10;i++){
squares.add(new Square());
}
Apply.apply(squares, Square.class.getMethod("rotate"));

Apply.apply(squares, Square.class.getMethod("resize", int.class), 5);


Apply.apply(new FilledList<Shape>(Shape.class,5), Shape.class.getMethod("rotate"));
Apply.apply(new FilledList<Shape>(Square.class,18), Shape.class.getMethod("resize", int.class),15);

SimpleQueue<Shape> shapeQ = new SimpleQueue<Shape>();
for(int i=0;i<5;i++){
shapeQ.add(new Shape());
shapeQ.add(new Square());
}
Apply.apply(shapeQ, Shape.class.getMethod("rotate"));
}

}

class Apply{
public static <T,S extends Iterable<? extends T>> void apply(S seq,Method f,Object...args){
try{
for(T t:seq){
f.invoke(t, args);
}
}catch(Exception e){
throw new RuntimeException();
}
}
}

class Shape{
public void rotate(){
System.out.println(this+" rotate");
}
public void resize(int newSize){
System.out.println(this+" resize"+newSize);
}
}

class Square extends Shape{}

class FilledList<T> extends ArrayList<T>{
public FilledList(Class<? extends T> type,int size){
try{
for(int i = 0;i<size;i++){
this.add(type.newInstance());
}
}catch(Exception e){
throw new RuntimeException();
}
}
}

class SimpleQueue<T> implements Iterable<T>{
private LinkedList<T> storage = new LinkedList<T>();
public void add(T t){
this.storage.offer(t);
}
public T get(){
return this.storage.poll();
}
@Override
public Iterator<T> iterator() {
return this.storage.iterator();
}

}


31 changes: 31 additions & 0 deletions src/btp/oneP/ArrayOfGenerics.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
package btp.oneP;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class ArrayOfGenerics {

public static void main(String[] args) {
//�����ڲ���ʵ�����������飬���ǿ��Դ����������������
List<String>[] ls;
List[] la = new List[10];
ls = (List<String>[])la;
ls[0] = new ArrayList<String>(Arrays.asList("hehe","keke","xixi"));
//ls[1] = new ArrayList<Integer>();

Object[] objects = ls;
objects[1] = new ArrayList<Integer>(Arrays.asList(1,2,3,4,5));
System.out.println(Arrays.toString(la));
System.out.println(la.getClass().getName());
System.out.println(Arrays.toString(ls));
System.out.println(ls.getClass().getName());
System.out.println(Arrays.toString(objects));
System.out.println(objects.getClass().getName());
List<BerylliumSphere>[] spheres = (List<BerylliumSphere>[])new List[10];
for(int i=0;i<10;i++){
spheres[i] = new ArrayList<BerylliumSphere>();
}
}

}
56 changes: 56 additions & 0 deletions src/btp/oneP/ArrayOptions.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package btp.oneP;

import java.util.Arrays;

public class ArrayOptions {

public static void main(String[] args) {
BerylliumSphere[] a = null;
BerylliumSphere[] b = new BerylliumSphere[5];
System.out.println(a);
System.out.println(b);
System.out.println(Arrays.toString(b));

BerylliumSphere[] c = new BerylliumSphere[4];
for(int i=0;i<c.length;i++){
if(null == c[i]){
c[i] = new BerylliumSphere();
}
}

BerylliumSphere[] d = {
new BerylliumSphere(),new BerylliumSphere(),new BerylliumSphere()
};
a = new BerylliumSphere[]{
new BerylliumSphere(),new BerylliumSphere(),new BerylliumSphere()
};
System.out.println("a.length:"+a.length);
System.out.println("b.length:"+b.length);
System.out.println("c.length:"+c.length);
System.out.println("d.length:"+d.length);

a = d;
System.out.println("a.length:"+a.length);

int[] e;
int[] f = new int[4];
System.out.println("f:"+Arrays.toString(f));
int[] g = new int[4];
for(int i=0;i<g.length;i++){
g[i] = i*i;
}
int[] h = {11,47,93};
System.out.println("f.length:"+f.length);
System.out.println("g.length:"+g.length);
System.out.println("h.length:"+h.length);

e = h;
System.out.println("e.length:"+e.length);
e = new int[]{1,2};
System.out.println("e.length:"+e.length);

char[] cs = new char[5];
System.out.println(Arrays.toString(cs));
}

}
16 changes: 16 additions & 0 deletions src/btp/oneP/ArrayTest.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
package btp.oneP;

public class ArrayTest {

public static void main(String[] args) {
//f({1,2,3});
f(new int[]{1,2,3});
}

static void f(int[] a){
for(int i:a){
System.out.println(i);
}
}

}
14 changes: 14 additions & 0 deletions src/btp/oneP/Byteset.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package btp.oneP;

import java.util.Arrays;
import java.util.HashSet;
import java.util.Set;

public class Byteset {
Byte[] possibles = {1,2,3,4,5,6,7,8,9};
Set<Byte> myset = new HashSet<Byte>(Arrays.asList(possibles));

Set<Byte> myset2 = new HashSet<Byte>(Arrays.<Byte>asList(new Byte[]{1,2,3,4,5,6,7,8,9}));
//The parameterized method <Byte>asList(Byte...) of type Arrays is not applicable for the arguments (Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer, Integer)
//Set<Byte> myset3 = new HashSet<Byte>(Arrays.<Byte>asList(1,2,3,4,5,6,7,8,9));
}
27 changes: 27 additions & 0 deletions src/btp/oneP/CRGWithBasicHolder.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package btp.oneP;

public class CRGWithBasicHolder {

public static void main(String[] args) {
Subtype st1 = new Subtype(),st2 = new Subtype();
st1.set(st2);
Subtype st3 = st1.get();
st1.f();
}

}

class BasicHolder<T>{
T element;
void set(T t){
element = t;
}
T get(){
return this.element;
}
void f(){
System.out.println(this.element.getClass().getName());
}
}

class Subtype extends BasicHolder<Subtype>{}
24 changes: 24 additions & 0 deletions src/btp/oneP/CaptureConversion.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
package btp.oneP;

public class CaptureConversion {

static <T> void f1(Holder<T> holder){
T t = holder.get();
System.out.println(t.getClass().getSimpleName());
}

static void f2(Holder<?> holder){
f1(holder);
}
public static void main(String[] args) {
Holder raw = new Holder<Integer>(1);
f1(raw);
f2(raw);
Holder rawBasic = new Holder();
rawBasic.set(new Object());
f2(rawBasic);
Holder<?> wildcarded = new Holder<Double>(1.0);
f2(wildcarded);
}

}
39 changes: 39 additions & 0 deletions src/btp/oneP/CheckedList.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
package btp.oneP;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;

public class CheckedList {

@SuppressWarnings("unchecked")
static void oldStyleMethod(List probablyDogs){
probablyDogs.add(new Cat());
}

public static void main(String[] args) {
List<Dog> dogs1 = new ArrayList<Dog>();
oldStyleMethod(dogs1);
System.out.println(dogs1);
List<Dog>s2 = Collections.checkedList(new ArrayList<Dog>(), Dog.class);
try{
oldStyleMethod(s2);
}catch(Exception e){
e.printStackTrace();
}

//work fine
List<Pet> pets = Collections.checkedList(new ArrayList<Pet>(), Pet.class);
pets.add(new Dog());
pets.add(new Cat());
pets.add(new Pet());
oldStyleMethod(pets);
System.out.println(pets);
}

}

class Pet{}
class Dog extends Pet{}
class Cat extends Pet{}

26 changes: 26 additions & 0 deletions src/btp/oneP/ClassCasting.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
package btp.oneP;

import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.util.List;

public class ClassCasting {

public void f(String...args) throws FileNotFoundException, IOException, ClassNotFoundException{
ObjectInputStream in = new ObjectInputStream(
new FileInputStream(args[0]));
//List<Widget> lwl = List<Widget>.class.ClassCasting(in.readObject());
//List<Widget> lw2 = (List<Widget>)List.class.cast(in.readObject());
List<Widget> lw2 = List.class.cast(in.readObject());
}

public static void main(String[] args) {
// TODO Auto-generated method stub

}

}

class Widget{}
30 changes: 30 additions & 0 deletions src/btp/oneP/ComparablePet.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
package btp.oneP;

public class ComparablePet implements Comparable<ComparablePet> {

@Override
public int compareTo(ComparablePet o) {
return 0;
}

}

//�����Ǵ���ģ�����ͬʱ�������Ͳ���
//class Cat extends ComparablePet implements Comparable<Cat>{}


class Hamster extends ComparablePet implements Comparable<ComparablePet>{
//��ʵ��͵�����д������Hamster��д�˸���ComparablePet�ķ���
@Override
public int compareTo(ComparablePet o) {
return 0;
}
}

//Gecko��Hanster��Ч��һ��
class Gecko extends ComparablePet{
@Override
public int compareTo(ComparablePet o) {
return 0;
}
}
Loading