I am not sure I am following, but you might be looking for a Map<Integer,String>. or Map<Integer,List<String>>. [have a look on List, and HashMap]
Map allows association of the key [Integer] to the value [String or List].
Map also allows fast lookup of key, and its attached value.
(*) You should use Map<Integer,List<String>> if you want to attach more then one String per Integer, or alternatively you can use apache commons MultiMap
I am not sure I am following, but you might be looking for a Map<Integer,String>. or Map<Integer,List<String>>. [have a look on List, and HashMap]
Map allows association of the key [Integer] to the value [String or List].
Map also allows fast lookup of key, and its attached value.
(*) You should use Map<Integer,List<String>> if you want to attach more then one String per Integer, or alternatively you can use apache commons MultiMap
Arrays can only contain one type. If that type happens to be Object then it can store Object and any of its sub-types, but that doesn't really sound like what you're trying to accomplish here.
It sounds like what you're describing is a 2D array to store database information, with each element in the array being a column in one of the rows. This isn't an array of records, it's an array of column data.
Instead, just store a one-dimensional array of records, where each element of the array is a reference to the entire DB row.
Videos
Instead of a List use a Map: That can "map" a value to another, and store it in a collection.
Something like this:
Map<String, Room> adjacentRooms = new HashmMap<>();
adjacentRooms.put("north", room);
adjacentRooms.get("east");
You may want to use constants, to make sure the values are "discrete".
It has a drawback, tho: it cannot assign more than 1 value to a key, that is more than 1 Rooms to a direction...
An ArrayList can only hold one data type. But I'm curious as to why you cant associate a string as a member in the Object you're talking about. Since you want a 2d arraylist, I'm assuming the string and "room" are related
Object foo = new Object();
foo.data = "your string"
adjacentRooms.add(foo);
access by
adjacentRooms.get(index).data
However, if you must, you can do a 2d ArrayList, but they get annoying
ArrayList<ArrayList<String> > list = new ArrayList();
access would be something like list.get(i).get(k) with 'i' referring to the index of ArrayList of Strings, and k referring to the index of a String in that 'i' ArrayList.
However, that structure does not store the "Object" you're talking about...
Hope that helps.
Rather use an object.
class MyEntry {
String foo;
int number;
}
MyEntry[] array = new MyEntry[10];
But if you absolutely must for some unfortunate reason, you can use two types - only through an Object supertype.
Object[][] arr = new Object[2][10];
arr[0][0] = "Foo";
arr[1][0] = new Integer(50);
No it is not possible . There can be only a single datatype for an array object. You can make a class having both the int and String as property and use it. Never use an Object[][] even if there is a temptation to do so, it is an evil workaround and hacks fail more than they succeeded . If Object was a sound technique then they wouldn't have introduced Generics for Collection !
It's slightly unclear what you're asking. In Java we don't actually have 2D arrays. Instead we have arrays of arrays. Similarly there is just 1D ArrayList which can contain different ArrayLists.
Another thing is the type stored. I would suggest writing your own class which would store the number Roman or Arabic.
public class RomanOrArabic {
private Long arabic;
private String roman;
public boolean isArabic(){
return arabic != null;
}
//add getters, smart setters, validators for String being Roman etc.
}
Then declare your ArrayList:
ArrayList<ArrayList<RomanOrArabic>> list = new ArrayList<>();
I saw there was some confusion so I thought I would post the solution here.
// The solution
Object RomanNumeralValues[][] = {
{500000, 999999, 900000, "D\u0305", "C\u0305M\u0305"},
{100000, 499999, 400000, "C\u0305", "C\u0305D\u0305"},
{50000, 99999, 90000, "L\u0305", "X\u0305C\u0305"},
{10000, 49999, 40000, "X\u0305", "X\u0305L\u0305"},
{5000, 9999, 9000, "V\u0305", "MX\u0305"},
{1000, 4999, 4000, "M", "MV\u0305"},
{500, 999, 900, "D", "CM"},
{100, 499, 400, "C", "CD"},
{50, 99, 90, "L", "XC"},
{10, 49, 40, "X", "XL"}
};
// Where the solution gets used
String romanNumeral = "";
for (Object rmRange[]: RomanNumeralValues) {
romanNumeral += extractRomanNumeralsBelowOneMillion(
(int)rmRange[0],
(int)rmRange[1],
(int)rmRange[2],
rmRange[3].toString(),
rmRange[4].toString()
);
}
I'm not comfortable with type casting just to pull the value out of an object array, but I guess it works. I'll leave the question open in case anyone has better solutions.