java placed in an array of values of different data types

method one:

Polymorphism

Defined array type, the parent class is defined, and stored into an array of a parent class is a subclass of

List<Object> list = new ArrayList<Object>();
list.add("abc");
list.add(123);
list.add(new HashMap<Integer,String>());


Or if the statement set, which does not declare generic type such as:

List list = new ArrayList();


The default is Object generics, with the above List <Object> same.

public class test2 {
  public static void main(String args[]) {
    father []a = new father[2];
    a[0] = new son();
    a[1] = new son2();
  }
}
class father{
  int i = 0; 
}
class son extends father{
  int x = 0;
}
class son2 extends father{
  int y = 0;
}

Method Two:

Map collection

Map set not inherited Collection interface, which provides the key to value mapping, Map can have the same key value

Method three:

Generics

 

Guess you like

Origin www.cnblogs.com/yangai/p/11060197.html