import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int M = sc.nextInt();
//array declaration
int [][] arr = new int[M][];
for(int i=0;i<M;i++)
{
int N = sc.nextInt();
arr[i] = new int[N];
for(int j=0;j<N;j++)
{
arr[i][j] = sc.nextInt();
}
System.out.println();
}
}
public static void Waveprint(int [][] arr)
{
for(int i=0;i<=arr.length-1;i++)
{
if(i%2==0)
{
for(int j=0;j<=arr[i].length-1;j++)
{
System.out.print(arr[i][j]+ ", ");
}
}
else
{
for(int j=arr[i].length-1; j>=0; jβ)
{
System.out.print(arr[i][j]+ ", ");
}
}
}
System.out.print(βENDβ);
}
}
Waveprint rowwise code
Take input of N in line following the input of M. And call the Waveprint method after you take input of array. I have attached the corrected code of the Main function.
public static void main(String args[]) {
Scanner sc = new Scanner(System.in);
int M = sc.nextInt();
int N = sc.nextInt();
// array declaration
int[][] arr = new int[M][N];
for (int i = 0; i < M; i++) {
// arr[i] = new int[N];
for (int j = 0; j < N; j++) {
arr[i][j] = sc.nextInt();
}
}
Waveprint(arr);
}