WeerTM
WeerTM

Reputation: 19

How to get range to first index when the loop reach end?

I've got List<List<Integer>> with several objects. Each of inner element contains randomly shuffled indexes, for example (it's only a part of indexes).

[1, 4, 5, 2, 0, 3, 6]
[4, 2, 5, 3, 1, 6, 0]
[0, 3, 6, 1, 2, 4, 5]

I've got also an array with some values (int[][] array)

enter image description here

And I need to do a loop for each element to get value from indexes and move forward by 1 index and when I reach last index I need to get value from this index and the first one. After that loop end and sum values. It might look difficult but pictrue will show what I mean. But I dont know how to do this (loop is required for each element, I'm gonna have a massive List of List<Integer> inside and every object gonna have multiple indexes.

enter image description here

I'm reading data from file and write it to array

List<String> result = new ArrayList<>();
        int[][] array = new int[][]{};

        try (Scanner sc = new Scanner(theFile)) {
            while (sc.hasNext()) {
                result.add(sc.nextLine());
            }
            int max = Integer.parseInt(result.get(0).trim());
            array = new int[max][max];
            for (int i = 1; i < result.size(); i++) {
                String[] tmp = result.get(i).trim().split(" ");
                for (int j = 0; j < tmp.length; j++) {
                    array[i - 1][j] = Integer.parseInt(tmp[j]);
                    array[j][i - 1] = array[i - 1][j];
                }
            }

 List<List<Integer>> collectionWithSubjects = new ArrayList<>();

        for (int i = 0; i < 40; i++) {
            List<Integer> sub = new ArrayList<>();
            sub = sequence(0,51);
            Collections.shuffle(sub);
            collectionWithSubjects.add(sub);
        }

Upvotes: 0

Views: 656

Answers (3)

azro
azro

Reputation: 54148

You may iterate over the alues of each List<Integer> then look them by pair (use % to go back at the beginning) and use them to index the 2d array

for (List<Integer> l : list) {
    int sum = 0;
    for (int i = 0; i < l.size(); i++) {
        int p1 = l.get(i % l.size());
        int p2 = l.get((i + 1) % l.size());
        sum += values[p1][p2];
    }
    System.out.println(sum);
}

With this as initial data

List<List<Integer>> list = List.of(List.of(1, 4, 5, 2, 0, 3, 6), List.of(4, 2, 5, 3, 1, 6, 0));

int[][] values = new int[][]{new int[]{31, 21, 34, 22, 67, 14, 41}, new int[]{17, 42, 31, 57, 26, 23, 52}, new int[]{5, 92, 52, 52, 31, 22, 62},
                 new int[]{17, 42, 31, 57, 26, 23, 52}, new int[]{5, 92, 52, 52, 31, 22, 62}, new int[]{31, 21, 34, 22, 67, 14, 41}, new int[]{5, 92, 52, 52, 31, 22, 62},};

it'll print

253
262

Upvotes: 0

Compass
Compass

Reputation: 5937

You've done a decent job of explaining the problem. It sounds like you're getting caught up on trying to do everything in a single for loop, rather than breaking down the problem into two pieces.

This is your statement:

I need to do a loop for each element to get value from indexes and move forward by 1 index and when I reach last index I need to get value from this index and the first one.

This can be divided into two pieces:

I need to do a loop for each element to get value from indexes and move forward by 1 index

This is a for loop that iterates from 0 -> size() - 1. If we go from 0 -> size() we get an overflow.

for(int i = 0; i < list.size() - 1; i++) {
    int firstCoord = list.get(i);
    int secondCoord = list.get(i+1);

    //do stuff
}

when I reach last index I need to get value from this index and the first one.

This is getting the last element and the first element.

int firstCoord = list.get(list.size() - 1);
int secondCoord = list.get(0);

Combine both together and you've got the framework for getting the coordinates.

for(int i = 0; i < list.size() - 1; i++) {
    int firstCoord = list.get(i);
    int secondCoord = list.get(i+1);

    //do stuff
}

int firstCoord = list.get(list.size() - 1);
int secondCoord = list.get(0);

//do stuff

I'll leave the actual implementation up to you.

Upvotes: 1

Green Cloak Guy
Green Cloak Guy

Reputation: 24691

// given array
int[][] array = [[31, 21, 34, 22, 67, 14, 41], 
                 [17, 42, 31, 57, 26, 23, 52], 
                 [5,  92, 52, 52, 31, 22, 62],
                 [17, 42, 31, 57, 26, 23, 52], 
                 [5,  92, 52, 52, 31, 22, 62], 
                 [31, 21, 34, 22, 67, 14, 41], 
                 [5,  92, 52, 52, 31, 22, 62]];
// given list of lists of randomly-ordered indices
List<List<Integer>> indexList = Arrays.toList([
                                    Arrays.toList([1, 4, 5, 2, 0, 3, 6]),
                                    Arrays.toList([4, 2, 5, 3, 1, 6, 0]),
                                    Arrays.toList([0, 3, 6, 1, 2, 4, 5])
                                ]);
// first, create a place to store the sums corresponding to each random list
List<Integer> sums = new ArrayList<Integer>();
// iterate over each of the lists of random elements
for(List<Integer> randomIndices: indexList){
    // create a running sum for this list
    int randomSum = 0;
    // iterate over each element of the randomized index list
    for(int j = 0; j < randomIndices.size(); j++){  
        // read the current and next index (using modulo to wrap around)
        current = randomIndices.get(j);
        next = randomIndices.get((j + 1) % randomIndices.size());
        // add the relevant index in array to the running sum
        randomSum += array[current][next];
    }
    // add the recorded randomSum to the sums list.
    // Its index is the same as the random list we just iterated over
    sums.add(randomSum);
}
System.out.println(sums);

Upvotes: 0

Related Questions