vector - ArrayList and more primitive types togheter in Java -
i need create java array list (precisly, vector) contain more 1 primitive types. example, see in every vector's row this:
row[0] --> "str1", 5, "str2", "str3", 3.5, 6, "str4"...etc... row[1] --> "str5", 6, "str6", "str7", 5.3, 8, "str8"...etc...
can me?
thanks!! :)
i think solution problem. made code works integer, string , float (the rest u can finish).
main (with yours values)
public static void main(string[] args) { //init arraylist<arraylist<something>> mainlist = new arraylist<>(); arraylist<something> sublist1 = new arraylist<>(); arraylist<something> sublist2 = new arraylist<>(); //fill array lists sublist1.add(new something("str1", 0)); sublist1.add(new something(5, 1)); sublist1.add(new something("str2", 0)); sublist1.add(new something("str3", 0)); sublist1.add(new something(3.5f, 2)); //etc sublist2.add(new something("str5", 0)); sublist2.add(new something(6, 1)); sublist2.add(new something("str6", 0)); sublist2.add(new something("str7", 0)); sublist2.add(new something(5.3f, 2)); //etc //add 2 list in main mainlist.add(sublist1); mainlist.add(sublist2); //iterator int = 0; (arraylist<something> sublist : mainlist) { i++; system.out.println("results list " + i); (something : sublist) { switch (something.gettype()) { case 0: { system.out.println("string value: " + (string) something.getvalue()); break; } case 1: { system.out.println("integer value: " + (integer) something.getvalue()); break; } case 2: { system.out.println("float value: " + (float) something.getvalue()); break; } } } } }
class something:
public static class { private object value; private int type; /** * constructor of class * * @param value * @param type type 0 string <br/> * type 1 integer <br/> * type 2 float .... */ public something(object value, int type) { this.value = value; this.type = type; } public object getvalue() { return value; } public void setvalue(object value) { this.value = value; } public int gettype() { return type; } public void settype(int type) { this.type = type; } }
output of main:
results list 1 string value: str1 integer value: 5 string value: str2 string value: str3 float value: 3.5 results list 2 string value: str5 integer value: 6 string value: str6 string value: str7 float value: 5.3
Comments
Post a Comment