-4

I tried to create object which it's size and how many is defined by the user but there is only one array output.Is there any way to create arrays in a loop?

public class Somehthing {    
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        Scanner sc2 = new Scanner(System.in);
        Random rnd = new Random();
        System.out.println("How many array?:");

        for (int j = 0; j <= sc.nextInt(); j++) {
            System.out.println("Define array size:");
            int[] dizi = new int[sc2.nextInt()];
            for (int i = 0; i <= dizi.length - 1; i++) {
                int deger = rnd.nextInt(1000000);
                dizi[i] = deger;
                System.out.println(array[j]);
            }
        }
    }
}
Janez Kuhar
  • 3,705
  • 4
  • 22
  • 45
Cemal
  • 1
  • 1
  • Looks like you want a 2-dimensional array. `int [][] array = new int[sc2.nextInt()][];` – 001 Oct 31 '19 at 19:22
  • 1
    by the way `i<=array.length-1` can be simplified to `i < array.length` since `<` will not include `array.length` – incapaz Oct 31 '19 at 19:26
  • 2
    you sure don't want `for(int j=0;j<=sc.nextInt();j++)` - `nextInt()` will be called before each iteration - `int arrayCount = sc.nextInt(); for (int j = 0; j < arrayCount; j++)` (also note: using `<`, not `<=`) – user85421 Oct 31 '19 at 19:26
  • 2
    You don't need 2 `Scanner`s - one is enough. – Janez Kuhar Oct 31 '19 at 19:26
  • 1
    Just my opinion: I do not like `new int[sc2.nextInt()]` its doing two non-trivial operations at one line; I prefer `int dim = sc2.nextInt(); new int[dim];` easier to read (IMO) and debug; I would also add some input checking (negative numbers?) – user85421 Oct 31 '19 at 19:33

2 Answers2

0

I'm not sure if I understand correctly, but if you want to create n number of arrays, then all you have to do is use a new data-structure to hold your arrays; such as an ArrayList (you could use a 2D array to store information, but multi-dimensional arrays tend to get very complicated, very quickly; this is simpler.

So this slight modification is needed:

public static void main(String[] args) {


  Scanner sc = new Scanner(System.in);
  Scanner sc2 = new Scanner(System.in);  //I don't understand why you used a second scanner
  Random rnd= new Random();
  ArrayList<Integer[]> arrays = new ArrayList<>();  //you must import java.utils.ArrayList;
  System.out.println("How many array?:"); 

  for(int j=0;j<=sc.nextInt();j++){
     System.out.println("Define array size:");
     Integer[] array = new int[sc2.nextInt()];
     for(int i=0;i<=array.length-1;i++){
       int value=rnd.nextInt(1000000);
       array[i]= value;
       System.out.println(array[j]);
       }
     arrays.add(array); //You are adding this array into your List of arrays.
   }
}

Remember to use the wrapper class Integer() with Lists, primitive types will not work. If you need to know why, then read this stack-overflow answer or this tutorial for Generics.

I did not spend the time to optimize your code, because I tried to only address your problem/question.

Soutzikevich
  • 991
  • 3
  • 13
  • 29
-1

A 2-dimensional array will work:

Scanner sc = new Scanner(System.in);
Random rnd= new Random();

System.out.println("How many array?:");
int[][] array = new int[sc.nextInt()][];
for(int j = 0; j < array.length; j++){
    System.out.println("Define array size:");
    array[j] = new int[sc.nextInt()];
    for(int i = 0; i < array[j].length; i++){
        array[j][i] = rnd.nextInt(1000000);
    }
    System.out.println(Arrays.toString(array[j]));
}
001
  • 13,291
  • 5
  • 35
  • 66