I managed to make a "ragged" array in two forms: an ordinary triangle and an inverted one. However, I can not figure out how to move the elements to the other edge. That is, how to make the elements be built in this way:

1st option:

0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 

Option 2:

 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 

My program: Full program code

 int count = 0; for(int i = 0; i < arr.length; i++){ for(int j = 0; j < arr[i].length; j++){ arr[i][j] = count++; } 

Result:

 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 

Inverted triangle:

  int[][]arr = new int[lines][]; for(int i = 0; i < arr.length; i++){ arr[i] = new int[lines - i]; } 

Result:

 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
  • You have given two identical pieces of code and state that they are doing different things. Your code only fills the array with a sequence of numbers from 0 to arr.length * arr[i].length , does not rearrange it and does not display the result anywhere. - iksuy
  • arr[i][arr[i].length - 1- j] = count++; so want? - MrFylypenko
  • Yes, I'm sorry, I didn’t print it right. - Alex
  • correct the code in question so that it is clear what you have done and what you want to do. - iksuy
  • Corrected according to your amendments. - Alex

1 answer 1

To output an array to the console, format the strings using the String.format or System.out.printf command simply by adding spaces to the number itself, for example System.out.printf("%4d", 15); will print " 15" , 4 characters, where the first 2 of 4 spaces. In your code, I fixed only the output to the console:

 public static void main(String[] args) { System.out.print ("Введите размер треугольного правого массива "); triangle2(new Scanner(System.in).nextInt()); System.out.print("Введите размер перевернутого правого массива "); revTriangle2(new Scanner(System.in).nextInt()); } static int[][] triangle2(int lines) { int[][] arr = new int[lines][]; for (int i = 0; i < arr.length; i++) { arr[i] = new int[i + 1]; } int count = 0; for (int i = 0; i < arr.length; i++) { for (int j = 0; j < arr[i].length; j++) { arr[i][j] = count++; } } for (int i = 0; i < arr.length; i++) { String str = ""; for (int j = 0; j < arr[i].length; j++) { str += String.format("%3d", arr[i][j]); } String format = "%" + lines * 3 + "s"; System.out.printf(format, str); System.out.println(); } return arr; } static int[][] revTriangle2(int lines){ int[][]arr = new int[lines][]; for(int i = 0; i < arr.length; i++){ arr[i] = new int[lines - i]; } int count = 0; for(int i = 0; i < arr.length; i++){ for(int j = 0; j < arr[i].length; j++){ arr[i][j] = count++; } } for(int i = 0; i < arr.length; i++){ String str = ""; for(int j = 0; j < arr[i].length; j++){ str += String.format("%3d", arr[i][j]); } String format = "%" + lines * 3 + "s"; System.out.printf(format, str); System.out.println(); } return arr; } 

Result in the console:

 Введите размер треугольного правого массива 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 Введите размер перевернутого правого массива 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14