How to take the first and last value from an arraylist inside an arraylist of arrylist using java

StackOverflow https://stackoverflow.com/questions/23602309

  •  20-07-2023
  •  | 
  •  

Pregunta

How to take the first and last value from an arraylist inside an arraylist of arrylist and check the adjacent values of other arraylist values in java?

for example:

This is my arraylist of arraylist

[[1,0],[2,0],[3,3],[1,0,0]]

This arraylist size can be any thing not consatant and the values indside this arraylist will varies.

What I need here is [1,0] from this value it should take the first value and last value and need to check with all the other values from the arraylist of arraylist. If the adjacent values are same then it should store in a different place and need to display it.

I need a output like this:

group :0

1,0

1,0,0

group:1

2,0

group:3

3,3

If the first and last values are same it should go to one group and remaining different group and it need to display all the values.

This is the code I tried but i am very new to java. so my code is not that good .please help me to find this output.

  for (int a = 0; a < pattrn.size(); a++) {
        // System.out.println(pat.size());
        first = pattrn.get(a);
        break;
    }
    System.out.println(first);
    String temp = first.toString().replace("[", "").replace("]", "");
    System.out.println(temp);
    StringTokenizer firstvar = new StringTokenizer(temp, ", ");
    firstvalue = firstvar.nextToken();
    System.out.println("first value.." + firstvalue);
    while (firstvar.hasMoreTokens()) {
        lastvalue = firstvar.nextToken();
        System.out.println("last value.." + lastvalue);
    }

using this code I am not able to find this output.

¿Fue útil?

Solución

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 ints 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.

Otros consejos

Well, idk about the rest, but this is how you get the first and the last of each part of the array.

public static int[][] myArray = {
    { 1, 2, 3 },
    { 1, 2, 3, 4 },
    { 1, 2, 3, 4, 5 },
    { 1, 2, 3, 4 },
    { 1, 2, 3 },
    { 1, 2, 3, 4, 5 },
    { 1, 2, 3, 4 },
    { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 },
    { 1, 2, 3, 4, 5, 6, 7 },
};

It's fairly simple to get the first and last, just run a loop over the array like so:

public static void main(String[] args) {
    for (int i = 0; i < myArray.length; i++) {
        int first = myArray[i][0]; // first value
        int last = myArray[i][myArray[i].length - 1]; // last
        System.out.println("["+i+"]First: "+first+", Last: "+last+"");
    }
}

Output would be:

[0]First: 1, Last: 3
[1]First: 1, Last: 4
[2]First: 1, Last: 5
[3]First: 1, Last: 4
[4]First: 1, Last: 3
[5]First: 1, Last: 5
[6]First: 1, Last: 4
[7]First: 1, Last: 10
[8]First: 1, Last: 10

Hope this helped atleast some ^-^

EDIT: Also adding an array to an array list works the same way:

public static ArrayList<int[]> myList = new ArrayList<int[]>();

    static {
        myList.add(new int[] { 1, 2 });
        myList.add(new int[] { 1, 2, 3 });
        myList.add(new int[] { 1, 2, 3, 4 });
        myList.add(new int[] { 1, 2, 3, 4, 5 });
        myList.add(new int[] { 1, 2, 3, 4, 5, 6 });
        myList.add(new int[] { 1, 2, 3, 4, 5, 6, 7 });
        myList.add(new int[] { 1, 2, 3, 4, 5, 6, 7, 8 });
        myList.add(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9});
        myList.add(new int[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 });
    }

    public static void main(String[] args) {
        for (int i = 0; i < myList.size(); i++) {
            int first = myList.get(i)[0]; // first value
            int last =  myList.get(i)[myList.get(i).length - 1]; // last
            System.out.println("["+i+"] First: "+first+", Last: "+last+"");
        }
    }

Output:

[0]First: 1, Last: 2
[1]First: 1, Last: 3
[2]First: 1, Last: 4
[3]First: 1, Last: 5
[4]First: 1, Last: 6
[5]First: 1, Last: 7
[6]First: 1, Last: 8
[7]First: 1, Last: 9
[8]First: 1, Last: 10
Licenciado bajo: CC-BY-SA con atribución
No afiliado a StackOverflow
scroll top