There is no need to convert your Lists into Strings.
The List class allows objects to be retrieved by indexes. So keeping in mind that indexes are 0 based for List in Java, you can do this:
List<List<Integer>> collection = new ArrayList<List<Integer>>();
//Some initialization to get to this: [[1,0],[2,0],[3,3],[1,0,0]]
//The [1,0] list can be retrieved by the index 0
List<Integer> list = collection.get(0);
//Then you can do the same thing on that list to get the first value
Integer integer = list.get(0); //This will be 1
Then to get the last value in a list, you can use the size()
method. This returns the size of the list. So in your case, that is 4. But remember that indexes start at 0, so to get the final element in a list, you use size() - 1
as the index.
//Then the following line will give you the last List of [1,0,0]
List<Integer> list = collection.get( collection.size() - 1 );
//Then you can do the same thing on that list to get the last value
//The size here will be 3 of course as this is the [1,0,0] list
Integer integer = list.get( list.size() - 1 ); //This will be 0
Of course, you can combine these with loops of int
s to loop though all of the indexes of a particular list and then do the comparisons.
I've written a program that does what you're looking for and placed it here. I've also tried to explain what I'm doing at each stage as it might make use of things you're not familiar with if you're new to Java.
Edit: Eventual Solution
The OP revealed in the comments that the elements in the lists must be Strings and not Integers as the items being processed may contain letters.
Also revealed was that the solution I had produced had a couple of issues. When testing it with the input [[1,0],[2,0],[3,3],[1,0,0],[2,0],[2,0,0],[0,0],[1,2]]
it became apparent there were issues around the group numbers being assigned giving NullPointerExceptions during printing. Also, there was an issue where some lists would not be grouped appropriately due to concurrent modification of lists.
Having fixed the above issues, I produced this version of my code which fully solved this issue.