0

what is the data structure that allows you to add different kinds of data (String, Float, int...) together? For example:

table tab = new table();
tab.add("abcdefgh");
int i=0;
tab.add(i);
float f;
tab.add(f);
...
MalakTN
  • 11
  • 4

2 Answers2

0

You can use either collections (lists or sets) or maps. Both are generic, so in your case you should use Object as a generic parameter:

List<Object> list = new ArrayList<>();
list.add(1);
list.add("hello");
list.add(new Date());

etc.

However it seems that you are on a wrong way. This is typically very bad practice to use untyped data. Java is a strongly typed language and java programmers tend to use this advantage.

You can also use Table from guava.

I'd recommend you to ask more specific question. I believe that you have a task and you think that storing data of different types in one place is a good idea. It is not.

I hope this helps.

AlexR
  • 114,158
  • 16
  • 130
  • 208
  • yes, I have to store: String (sentence), Float(score or weight of sentence) and String(kind of sentence), so I have to store its together to call its – MalakTN Feb 27 '14 at 17:00
0

If I have understand your question and you need to store in a list of related values, create an object

class MyObject{
    private String sentence;
    private Float score;
    private String kind;
    // getters and setters
}

where you can save the related values, and then add your objects to a Collection.

For example:

List<MyObject> list = new ArrayList<MyObject>();

MyObject myObject = new MyObject();
myObject.setSentence("sentence");
// initialize object fields

list.add(myObject);
gipinani
  • 14,038
  • 12
  • 56
  • 85
  • thanks, it's may be a solution but in my task each variable is taken from a different class, I will try with this. – MalakTN Feb 27 '14 at 17:16