Reputation: 69
Hey guys ive read everything and i still cant get it. your my last hope.
All i need is a multidimensional arraylist that can store both a string and number at the same time. Please help.
Upvotes: 0
Views: 71
Reputation: 19466
What are the information you want to store? What do they represent? I doubt you just want to store some number and some string without context. Find out what the context is, what they represent, then extract a class.
Is it a student's name and age?
class Student {
private int age;
private String name;
public Student(String name, int age) {
this.name = name;
this.age = age;
}
...
}
Is it a car manufacturer and production year?
class Car {
private int productionYear;
private String manufacturer;
...
}
Once you've extracted your class, you can just create an ArrayList
of your type, something like this:
ArrayList<Car> cars = new ArrayList<Car>();
cars.add(new Car("Toyota Avalon", 2014));
cars.add(new Car("Fiat Focus", 1982));
Upvotes: 2
Reputation: 41188
The only way to do that would be to have the list contain Object, and then check the type of objects after you pull them out.
There is almost certainly a better architecture though (separate lists, maybe using a map, maybe storing an object that itself contains both a number and a string) etc. Unfortunately you haven't given enough information to let us give any guidance on that...
Upvotes: 0