public class Memory3 { static int[][] N = new int[0][]; int NLengh; void add(int[] i) { int[][] N22 = new int[1][]; boolean isSorted = false; int[][] N3 = new int[NLengh + 1][]; N3 = Arrays.copyOf(N, N3.length); N3[N3.length - 1] = i; NLengh++; N = N3; if (N.length != 1) { while (!isSorted) { isSorted = true; for (int k = 1; k < NLengh; k++) { if (N[NLengh - 1 - k][0] >= N[NLengh - k][0]) { isSorted = false; N22[0] = N[NLengh - 1 - k]; N[NLengh - 1 - k] = N[NLengh - k]; N[NLengh - k] = N22[0]; } } } } } public static void main(String[] args) { Memory3 M = new Memory3(); int[][] N22 = new int[6][]; N22[0] = new int[]{7, 7, 3}; N22[1] = new int[]{6, 8, 6, 4}; N22[2] = new int[]{5, 2, 6, 5}; N22[3] = new int[]{3, 8, 6, 7}; N22[4] = new int[]{1, 2, 6, 2}; N22[5] = new int[]{0, 7, 6, 4, 8}; for (int s2 = 0; s2 < N22.length; s2++) { M.add(N22[s2]); } for (int r2 = 0; r2 < N.length; r2++) { for (int r = 0; r < N[r2].length; r++) { System.out.println("N[" + r2 + "][" + r + "]" + N[r2][r] + ""); } System.out.println("\n"); } } } 

It is necessary that I could transfer arrays of various dimensions to the add method and not just one-dimensional int arrays and already in the array method to increase the number of elements (references) to arrays. maybe use a generalization? Thank.

  • Directly do not do this. You can declare a parameter of type Object then you can transfer anything. Generics with arrays work poorly. You can create your own class and hide in it the logic of access to the elements. - talex
  • @talex 1) why? You can do it ... But there are a number of issues that need to be thought through. Yes, and implementation is tough. 2) you need to understand what the author really wanted. In that block of code that he threw, two-dimensional arrays are hardcoded. Why it is a question of “various dimensions” is not entirely clear 3) The question is more to the author. What is bad implementation through the collection? Or are arrays explicitly spelled out in the task? - Anton M
  • @AntonM 1) I have no idea how to write type. 2) This is always the main thing :) - talex
  • @talex recursively working with object th, at each new iteration checking whether it is an object or not ( isInstance() to help). If the dimension is greater than or equal to the current one, it is still clear what to do. And what to do with a smaller dimension, for me personally, is a big question. - Anton M
  • @talex can do something like class ArrayOrObject , which by get checks whether the array is created, otherwise it returns an object. But again, somehow we have to work with this ... - Anton M

2 answers 2

Here is an example of a custom class that allows working with arrays of arbitrary (but fixed) dimension.

 public class App { public static void main(String[] args) { Cust arr = new Cust(2, 2); arr.set(1, 0, 0); arr.get(0, 0); } } class Cust { private final Object arr; private final int dimension; Cust(int... dimensions) { if (dimensions.length == 0) { throw new IllegalArgumentException("rank < 1"); } dimension = dimensions.length; arr = createArr(dimensions, 0); } private Object createArr(int[] dimensions, int index) { int currentRank = dimensions[index]; if (index == dimensions.length - 1) { return new int[currentRank]; } Object[] tmp = new Object[currentRank]; for (int i = 0; i < currentRank; i++) { tmp[i] = createArr(dimensions, index + 1); } return tmp; } int get(int... indexes) { if (indexes.length != dimension) { throw new IllegalArgumentException(); } int[] inner = getInner(arr, indexes, 0); return inner[indexes[indexes.length - 1]]; } void set(int value, int... indexes) { if (indexes.length != dimension) { throw new IllegalArgumentException(); } int[] inner = getInner(arr, indexes, 0); inner[indexes[indexes.length - 1]] = value; } private int[] getInner(Object o, int[] indexes, int index) { if (indexes.length - 1 == index) { return (int[]) o; } else { Object[] tmp = (Object[]) o; return getInner(tmp[indexes[index]], indexes, index + 1); } } } 
  • Apparently I didn’t describe the problem a bit. - Maxa
  • Actually, I need this for the following: - Maxa
  • {int [] [] N = new int [7] []; N [0] = new int [] {0, 1, 2}; N [1] = new int [] {1, 0, 1}; N [2] = new int [] {1, 1, 1}; N [3] = new int [] {0, 1, 1}; N [4] = new int [] {2, 0, 1}; N [5] = new int [] {0, 0, 1}; N [6] = new int [] {0, 0, 0}; } - Maxa
  • // int [] [] [] N22 = new int [0] [] [] // this will change the dimension of the array and change the placement address of /// elements as the array is filled // array int [1] [1] [ 1] -address N22 [0] [0] N [0] // array int [2] [1] [1] -address N22 [0] [0] [0] N [0], -address N22 [1 ] [0] [0] N [1] // array int [2] [2] [1] - address N22 [0] [0] [0] N [0], -address N22 [1] [0] [0] N [1] // address N22 [1] [0] [1] N [2] // array int [2] [2] [2] - address N22 [0] [0] [0] N [3], address N22 [0] [0] [1] N [1] // address N22 [0] [0] [0] N [2], address N22 [0] [0] [0] N [ 3] // and so on - Maxa
 { int [][] N =new int[7][]; N[0]=new int[] { 0, 1, 2 }; N[1]=new int[] { 1, 0, 1 }; N[2]=new int[] { 1, 1, 1 }; N[3]=new int[] { 0, 1, 1 }; N[4]=new int[] { 2, 0, 1 }; N[5]=new int[] { 0, 0, 1 }; N[6]=new int[] { 0, 0, 0 }; } int [][] [] N22 = new int [0][] [] // так будет изменяться размерность массива и зменяться адрес размещения ///элементов по мере заполнения массива // массив int [1][1][1] -адрес N22[0][0] N[0] // массив int [2][1][1] -адрес N22[0][0][0] N[0] , -адрес N22[1][0][0] N[1] // массив int [2][2][1] - адрес N22[0][0][0] N[0] , -адрес N22[1][0][0] N[1] // адрес N22[1][0][1] N[2] // массив int [2][2][2] - адрес N22[0][0][0] N[3] , адрес N22[0][0][1] N[1] // адрес N22[0][0][0] N[2] , адрес N22[0][0][0] N[3] // и т.д 
  • the dimension of the array to be filled is presumably up to 10 investments i.e. int [] [] [] [] [] [] [] [] [] [] or more so it is important that there are no references to empty arrays - Maxa