An important note about PHP is that almost everything is treated as a hashmap. That means an 'array' is really a map from some key to a value: that key can be anything because PHP is weakly typed.
Java is a "strictly typed language", meaning it's concept of an array is simply an indexed sequence of elements:
String arr[] = new String[3];//This array will be numerically indexed by integers and only integers.
//Note, too, that it will be length three, and never any other length.
Note that the above uses an 'array primitive', where the array does not derive from Java's Object
. You can use a full class by using a List
class-based object.
If you want to get the same functionality as in PHP you have to use a map of some sort. For instance:
Map<String, Object> arr = new HashMap<String, Object>();
for (int x = 0; x < 10; x++) {
arr.put(Integer.toString(x), "title" + x);
}
Note that Maps in Java are not 'ordered'. Note that we are also cheating here by casting x
to a string so that the Map will accept it as a key: because of strict typing the above declaration will only take String
objects as a key. You could change the definition to take any Object, but then it will be even more difficult to reason about what that array is.
Because your example is a multi-dimensional array, you will have to decide in Java what represents each of those dimensions. Some possibilities:
List<Integer, Map<String, Object>> useLists;
Map<String, Object> usePrimitiveArrays[];