For a web developer who used mostly PHP and JavaScript for several years, plunging into Java can be challenging especially when it comes to data types.
PHP and JavaScript are loosely typed languages, while Java is strict about types.
I was told that in Android, List<String> is very commonly used, even more than arrays. And now I’ve come to the point when I need to use a 2-dimensional List<String>.
Here’s a sample code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 |
List<List<String>> listOfList = new ArrayList<List<String>>(); List<String> x = new ArrayList<String>(); x.add("Hello"); x.add("world!"); listOfList.add(x); List<String> y = new ArrayList<String>(); y.add("1"); y.add("4"); y.add("3"); listOfList.add(y); for(List<String> ls : listOfList) { System.out.println(Arrays.deepToString(ls.toArray())); } Log.d("serialized", listOfList.toString()); |
Output of System.outprintln(Arrays.deepToString(ls.toArray()));
is this:
1 2 |
I/System.out: [Hello, world!] I/System.out: [1, 4, 3] |
The output of Log.d("serialized", listOfList.toString());
is this:
1 |
D/serialized: [[Hello, world!], [1, 4, 3]] |