0

At first sory for my bad english :)

I want create new list (Arraylist, hashmap etc.. which is appropriate) this list have 6 field for example;

ArrayList<Integer, Integer, String, Integer, String, Integer> nData = new ArrayList<>();

this is not correct i know but i need list has <int, int, String, int, String, int> but how? thank you

kibar
  • 822
  • 3
  • 17
  • 37
  • You should read this, it will help you get more answers (not that you really need them in this case) [how to accept an answer](http://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) – JTMon Sep 03 '12 at 13:05
  • Oh. My. God. It's the first time when I'm seen such application of generics :) – Dmitry Zaytsev Sep 03 '12 at 13:06

5 Answers5

5

Create a Class which wraps all those fields, and add instances of this class to the ArrayList.

For example:

public MyClass {
  private Integer field1;
  private Integer field2;
  private String field3;
  private Integer field4;
  private String field5;
  private Integer field6;

  // constructor

  // getters/setters
}

And then:

List<MyClass> nData = new ArrayList<MyClass>();
MyClass instance = new MyClass(1, 2, "String1", 4, "String2", 5);
nData.add(instance);
João Silva
  • 89,303
  • 29
  • 152
  • 158
1

I think best suggestion depends on details & constraints of your design, but it seems like order of data types is constant. Thus, HashMap<int,Object[]> or Hashtable<int,Object[]> may be useful for your purpose. You can see differences between HashMap and HashTable here.

Community
  • 1
  • 1
Evren PALA
  • 56
  • 3
0

Create a new class that has the fields you want and make an ArrayList of that class.

Taymon
  • 24,950
  • 9
  • 62
  • 84
0

Create a class which has all these field, say MyClass, like this:-

class MyClass
{
  Integer a;
  Integer b;
  String c;
  Integer d;
  String e;
  Integer f;
}

After that create ArrayList like this:-

ArrayList<MyClass> nData = new ArrayList<MyClass>();

You can add the values in your list as:-

MyClass m = new MyClass(1,2,"abcd",3,"bcde",4);
nData.add(m);
Jainendra
  • 24,713
  • 30
  • 122
  • 169
0
  1. create class with holds those fields, or
  2. use Integer instead int, and create List and then just cast it to required type

    List<Object> list = new ArrayList<Object>();
    for (Object o : list)
    {
    if (o instanceof Integer)
    System.out.println( "" + ((Integer) o).intValue() + " is integer");
    else 
    System.out.println( "" + ((String) o) + " is String");
    }
    
user902383
  • 8,420
  • 8
  • 43
  • 63