Динамическое переименование массива каждый раз учитывается во время выполнения.

В основном каждый раз, когда запускается цикл for, я хочу создать массив с новым именем. Таким образом, в конце программы я смогу распечатать каждый массив, содержащий все отдельные переменные внутри. Вот мой код до сих пор с созданным массивом, однако каждый раз, когда оператор цикла запускается, он перезаписывает массив.

package clark.motors.car.check.program;

import java.util.*;

public class ClarkMotorsCarCheckProgram {

    public static void main(String[] args) 
    {


        //System.out.println(test);
        Scanner Input = new Scanner(System.in);
        int inputcars = 0;
        //List<String> reg3 = new ArrayList<>();

        boolean newcar=false;
        //int inputcars;

        System.out.println("Welcome to Clarke's Automobile Car-Check Program");
        System.out.println("Please specify 'Yes' or 'no' if you wish to add a new char to the current list");
        System.out.println("");
        String text = Input.nextLine();
        if(text.equals("yes"))
        {
            System.out.println("Please specify How many you wish to add");
            System.out.println("");
            inputcars = Input.nextInt();
            Input.nextLine();
            //int u = inputcars;
            //String strI = "" + u;
        }
        for (int i=0;i<inputcars;i++)
        {
            String strI = "" + i;
            String test = "person" + i;
            Scanner Input2 = new Scanner(System.in);
            System.out.println("Please specify the Regristration of the next car");
            String reg = Input2.nextLine();

            System.out.println("Please specify the color of the next car");
            String color = Input2.nextLine();

            System.out.println("Please specify how many wheel are on the nect car");
            int wheels = Input2.nextInt();

            System.out.println("Please specify how many doors are on the next car");
            int doors = Input2.nextInt();

            System.out.println("Please specify the make of the next car");
            String make = Input2.nextLine();
            String make2 = Input2.nextLine();
            System.out.println("Please specify the state of the next car. 'scrap', 'repairs', 'working'");
            String state = Input2.nextLine();

            // ClarkMotarsCreateCars reg2  = new ClarkMotarsCreateCars(reg,color,wheels,doors,make,state);
            //Map<String, String> details = new HashMap<>();

            //List<String> details = new ArrayList <> ();
        String details [] = {reg, color, make, state}; 
        int car2 [] = {wheels, doors};
        System.out.println("The reg of the car is: "+reg);
        System.out.println("The colour of the car is: "+color);
        System.out.println("The make of the car is: "+make2);
        System.out.println("The state of the car is: "+state);
        System.out.println("The amount of wheels on the car: "+wheels);
        System.out.println("The amount of doors on the car: "+doors);
        }

    }
    public String details [] = {reg, color, make, state}; 

}

person JamesGratrix    schedule 12.11.2015    source источник


Ответы (1)


ArrayList<ClarkMotarsCreateCars> cars = new ArrayList<>();

for(..){
     cars.add(new ClarkMotarsCreateCars(reg,color,wheels,doors,make,state));
}

Это даст вам массив объектов и не перезапишет первую запись.

person blur0224    schedule 12.11.2015
comment
Это прекрасно работает, однако теперь есть простой способ распечатать это, что позволяет нам распечатать первый объект в первом списке в массиве. - person JamesGratrix; 13.11.2015
comment
stackoverflow.com/ вопросы/13001427/ - person blur0224; 13.11.2015