Create multiple arrays using a for loop

2019-09-04 06:44发布

问题:

I would like to make a program where the user can input the number of variables and fill every variable with certain values. For example, the User inputs that he/she wants to make 10 arrays, then the User inputs that the first array should have 5 elements and the User fills that array with values, then the User wants the second array to have 4 elements and does the same and so on.

This is the code I was using, but it doesn't work:

public static void main(String[] args){
    Scanner s = new Scanner(System.in);
    System.out.println("Enter the numbers of variables: ");
    int i = s.nextInt();

    for(int j = 0;j < i;j++){
        int[] var = new int[j];
        System.out.println("Enter the number of values: ");
        int p = s.nextInt();
        for(int q = 0;q < p;p++){
            int n = s.nextInt();
            var[q] = n;
        }
    }
}

And how could I compare these arrays that the user inputs?

回答1:

The problem is that each time you are creating the array. try this:

Scanner s = new Scanner(System.in);
System.out.println("Enter the numbers of variables: ");
int i = s.nextInt();
int[][] var = new int[i][];
for(int j = 0;j < i;j++){

    System.out.println("Enter the number of values: ");
    int p = s.nextInt();
    var[j] = new int[p];
    for(int q = 0;q < p;p++){
        int n = s.nextInt();
        var[j][q] = n;
    }
}

Instead of creating a one dimensional array, you create a jagged array. Essentially, a 2d array is an array of arrays. that way the user inputs the number of arrays (i) and then continues to fill the arrays.

To check whether two collections have no commons values, you can use

Collections.disjoint();

For other operations, you can look here



回答2:

This should work (with bidimensionnal array)

    public static void main(String[] args){
        Scanner s = new Scanner(System.in);
        System.out.println("Enter the numbers of variables: ");
        int i = s.nextInt();
        int[][] var = new int[i][];
        for(int j = 0;j < i;j++){
            System.out.println("Enter the number of values: ");
            int p = s.nextInt();
            var[j] = new int[p];
            for(int q = 0;q < p;q++){
                int n = s.nextInt();
                var[j][q] = n;
            }
        }
    }

You have to replace the incrementation in the second loop too ("q++" instead of "p++")



回答3:

This should work and solve their first point

    Scanner s = new Scanner(System.in);
    System.out.println("Enter the numbers of variables: ");
    int i = s.nextInt();

    int[][] var = new int[i][];

    for(int j = 0;j < i;j++){
         System.out.println("Enter the number of values: ");
         int p = s.nextInt();
         while (p>0)
         {
            var[j] = new int[p];
            for(int q=0;q < p;q++){
               System.out.println("Value number : " +(q+1) + " For Array Number "+  (j+1));
               int n = s.nextInt();
               var[j][q] = n;
            }
             p-=1;
         }
    }