简体   繁体   中英

How do i add data from a variable to an array?

So basically I created an array.

int[] idarray = new int [5];

Now I want the user to input something.

idd = JOptionPane.showInputDialog ("\nEnter ID number:\n\n");
id = Integer.parseInt(idd);

and whatever the data may be would go to the array idarray .

How do I add the data from the variable into the array?

Firstly, you should be using a List<Integer> (which would allow you to accept unlimited user input), but if you must use an array, this will work to fill your array from user input:

for (int i = 0; i < idarray.length; i++) {
    idd = JOptionPane.showInputDialog ("\nEnter ID number:\n\n");
    id = Integer.parseInt(idd);
    idarray[i] = id;
}

idarray[i] = id; // of course you have to decide i yourself

if you want to simply appending things, you could use ArrayList instead of plain array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM