Good day.

There is a list of arrays with indexing starting from zero. I also need that both in the list and in its arrays the indexing starts from 1. What is the simplest way to do this?

Each next array in the list is created by cloning one of the previous ones.

There must be only one class in the program - the main one, such is the specifics of the task.

Can I list the first element (i.e, with index 0) in advance before entering arrays to put null so that the indexation of non-empty arrays starts from one?

 public class Main implements Cloneable { public static void main(String[] args){ List<int[]> a = new ArrayList<int[]>(); Scanner reader = new Scanner(System.in); int n; for( ; ; ) { n = reader.nextInt(); if(n<100000) break; } int[] a0 = new int[n]; for (int i = 0; i < n; i++) a0[i] = reader.nextInt(); a.add(a0); int m; for( ; ; ) { m = reader.nextInt(); if(m<100000) break; } String[] request = new String[m]; for (int i = 0; i < m; i++){ request[i] = reader.next(); if ( request[i].equals("create")) { int version = reader.nextInt(); int position = reader.nextInt(); int symbol = reader.nextInt(); create(a, position, version, symbol); } else if (request[i].equals("get")) { int version = reader.nextInt(); int position = reader.nextInt(); System.out.println(get( a, position, version)); } } } public static void create(List<int[]> a, int position, int version, int symbol){ a.add((a.get(version)).clone()); int last=a.size()-1; a.get(last)[position]=symbol; } public static int get(List<int[]> a, int position, int version){ return a.get(version)[position]; } } 
  • one
    Use the List.add (index, element) method; in it you can specify the index of the element add as an option the field int counter = 1; and on the site use List.add (counter ++, (a.get (version)). clone ()) ... you can do the same thing with receiving. ) Good luck - Peter Slusar
  • you can of course. but it will be wrong. just take a unit when you need it. - etki

1 answer 1

From the point of view of encapsulation, it is completely unimportant how exactly your methods will receive data from the array inside themselves and what to do with them, only the interface that “sticks out” outside is important. Therefore, if there is a need for "external users" to present the numbering starting with one, the most correct would be to subtract the unit inside the corresponding methods (to which the index is transferred). If you write null as the first element, then you will have a discrepancy: the number of collection elements is usually equal to size()-1 , and your first will be null, and the number of significant elements will be size()-2 , the code will be confused and incomprehensible. But no gain is not visible.