Java Program to Sort 2D Array Across Left Diagonal

10 Oct, 2021
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>();
Algorithm:
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.
Syntax:
Vector.removeAll(Vector);  
B. Collections.sort(): This method is used to sort the vector.
Syntax:
Collections.sort(Vector) ;
C. add(): It to add elements in the vector.
Syntax:
Vector.add(value) ;
D. get(): This method will get an element of Vector stored at a particular index.
Syntax:
Vector.get(3);
Example:
Java
// Java Program to Sort 2D Array Across Left Diagonal // Importing required classesimport java.io.*;import java.lang.*;import java.util.*; // Main classclass GFG {     // Main driver method    public static void main(String[] args)        throws java.lang.Exception    {         // Custom input 2D array        int[][] arr            = { { 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 }, { 1, 4, 7, 2, 8 } };         // Display message        System.out.println("Matrix without sorting \n");         // Nested for loops to display 2D matrix        for (int i = 0; i < 5; i++) {            for (int j = 0; j < 5; j++) {                 // Printing elements                System.out.print(arr[i][j] + " ");            }             // By now we are done with one row so            // new line is needed            System.out.println();        }         // Creating an object of Vector class        Vector<Integer> v = new Vector<>();         // Adding elements of diagonal in vector        // using add() method        for (int i = 0; i < 5; i++) {            v.add(arr[i][i]);        }         // Sorting elements in vector        // using Collections.sort() method        Collections.sort(v);         // Sorted elements are pushed back from vector to        // row using get() method        for (int j = 0; j < 5; j++) {            arr[j][j] = v.get(j);        }         // Display message        System.out.println("Matrix after sorting \n");         // Nested for loops to display 2D matrix        for (int i = 0; i < 5; i++) {            for (int j = 0; j < 5; j++) {                System.out.print(arr[i][j] + " ");            }            System.out.println();        }    }}
  
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 
 
anikakapoor
Blogathon-2021
Java-Array-Programs
Blogathon
Java
Java Programs
Java
Writing code in comment?
Please use ide.geeksforgeeks.org,
generate link and share the link here.
How to Import JSON Data into SQL Server?
SQL Query to Remove Decimal Values
Stratified Sampling in Pandas
SQL Query to Convert Datetime to Date
Python program to convert XML to Dictionary
Arrays in Java
Split() String method in Java with examples
Arrays.sort() in Java with examples
Object Oriented Programming (OOPs) Concept in Java
Reverse a string in Java