Question

I need to create a Java array list (precisly, a vector) that contain more than one primitive types. For example, I would like to see in every vector's row about this:

row[0] --> "str1", 5, "str2", "str3", 3.5, 6, "str4"...etc...
row[1] --> "str5", 6, "str6", "str7", 5.3, 8, "str8"...etc...

Can you help me?

Thanks!! :)

Was it helpful?

Solution

I think this is a solution to your problem. I made a code that works for integer, string and 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 the two list in the main
        mainList.add(sublist1);
        mainList.add(sublist2);
        //Iterator
        int i = 0;
        for (ArrayList<Something> sublist : mainList) {
            i++;
            System.out.println("Results from list " + i);
            for (Something 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 Something {

        private Object value;
        private int type;

        /**
         * Constructor of Something 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 the main:

Results from list 1
String value: str1
Integer value: 5
String value: str2
String value: str3
Float value: 3.5
Results from list 2
String value: str5
Integer value: 6
String value: str6
String value: str7
Float value: 5.3

OTHER TIPS

You can use list of list..like below

    Vector<Vector<Object>> list = new Vector<Vector<Object>>();
    Vector<Object> row1 = new Vector<Object>();
    row1.add(5);
    row1.add("John");

    Vector<Object> row2 = new Vector<Object>();
    row1.add(1.1);
    row1.add("Aby");

    list.add(row1);
    list.add(row2);

Java generics cannot contain primitive types. You have to use corresponding object type. e.g. Integer or Float.

To put objects of many types into a container declare it to hold a supertype:

Vector<Object> v = new Vector<Object>();
v.add("String");
v.add(new Integer(5));

Mind that when you do v.get(0) you will receive an object of type Object. You have to know to what type you want to cast it back.

To check a runtime type of an object you can use Object.getClass(), or compare it with a known type using instanceof operator like this:

if(object instanceof String){
    // do something with a String
}else{
    // do something with a Number (Integer etc.)
}

There is no neat-and-easy way to store mixed-type data in a uniform manner in Java. So you better define List of Object to store row contents and prepare to write huge IF:

if (row[i] instanceof String) {
 ...
} else if (row[i] instanceof Integer) {
 ...
}

and so on. I did that when worked with protobuf, emulating mix-typed fields.

And more: Forget about primitives, if you are going to use List. Java generics are incompatible with primitive types, unfortunatelly.

As you items in array represent some logical order you can try to create a representation of them in form a data structure like class.

class NameOfStructure {
  private String str1;
  private int    int1;
  private String str2;
}

An later put that structure into array.

NameOfStructure[] rows = new NameOfStructure[size];

NameOfStructure row = NameOfStructure[0];

String str1 = row.str1;
 int   int1 = row.int1;

An alternative option is to create array type of Object[], but then you must cast the type to expected after retrieving an item from array.

Object[] row = new Object[size];
...
String  str1 = (String)  row[0];
Integer int1 = (Integer) row[1];
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top