Java Program to Sort 2D Array Across Left Diagonal
The Vector class implements a growable array of objects. Vectors basically fall in legacy classes but now it is fully compatible with collections. It is found in java.util package and implements the List interface, so we can use all the methods of List interface here. This program is used to Sort the 2D array Across Left Diagonal. We will use the concept of vector to sort Left Diagonal prior to which refer to below illustration as follows.
Illustration: Creating a Vector
Vector(): Creates a default vector of the initial capacity is 10.
Vector<E> v = new Vector<E>();
- Traverse Diagonal element one by one. Condition is (i==j) or ( arr[i][i])
- Add elements of Diagonal in vector v.
- Sort the vector.
- Push back the sorted elements from vector to Diagonal.
Now let us discuss functions that will be used in the above approach prior to landing upon the implementation part.
A. removeAll(): The java.util.vector.removeAll(Collection col) method is used to remove all the elements from the vector, present in the collection specified.
B. Collections.sort(): This method is used to sort the vector.
C. add(): It to add elements in the vector.
D. get(): This method will get an element of Vector stored at a particular index.
Matrix without sorting 5 2 0 7 1 3 4 2 9 14 5 1 3 5 2 4 2 6 2 1 0 6 3 5 1 Matrix after sorting 1 2 0 7 1 3 2 2 9 14 5 1 3 5 2 4 2 6 4 1 0 6 3 5 5