Paul
Paul

Reputation: 159

How to swap elements in ArrayList<ArrayList<Integer>>?

I'm trying to swap elements in ArrayList<ArrayList<Integer>>. I wanted to know either an inbuilt function if present any or brute-force approach to solve : swap(mat.get(i).get(j),mat.get(j).get(i+1)) swapping these 2 elements in a ArrayList<ArrayList<Integer>>.

Upvotes: 2

Views: 263

Answers (1)

I don't believe there is a built-in function to swap these two elements.

A brute force solution would have to store one value in another variable and then swap them in O(1) time.

Something like:

void swap(int i, int j){
    int val = bigList.get(i).set(j, bigList.get(i+1).get(j));
    bigList.get(i+1).set(j, val);
}

--Where 'bigList' is the parent ArrayList containing ArrayLists.

Hope this helps!

Upvotes: 1

Related Questions