aspose file tools*
The moose likes Beginning Java and the fly likes How do you add objects to arrays? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "How do you add objects to arrays?" Watch "How do you add objects to arrays?" New topic
Author

How do you add objects to arrays?

Jacob Coddaire
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

I'm creating a Connect Four program, and the first part of it prompts the user for the number of players. The minimum allowed is two and the max is four. Once that is determined, it then progresses to allow the users to input their names. These names need to be stored in an array (or arrayList, it doesn't matter). Here's my code so far. Keep in mind that some string isn't being used yet. ANy suggestions?


String name, start, size, win;
String playerSTR;
int player;

String[] array = {null, null, null, null};

playerSTR = JOptionPane.showInputDialog("How many players?");
player = Integer.parseInt(playerSTR);

for (int i=0; i<player; i++)
{
name = JOptionPane.showInputDialog("Enter a name:");
// this is where the array should add name

}

}

}[/code]
Jacob Coddaire
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1691
    
  12

See if you find some help in this Array tutorial.....


Edit: And WELCOME to JavaRanch!!


When you do things right, people won't be sure you've done anything at all.
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Welcome to the Ranch!

To access elements of an array, use the array name followed by the index enclosed in brackets. For example...

array[0] = "Shannon";
array[1] = "Tina";


"We're kind of on the level of crossword puzzle writers... And no one ever goes to them and gives them an award." ~Joe Strummer
sscce.org
marc weber
Sheriff

Joined: Aug 31, 2004
Posts: 11343

Jacob Coddaire wrote:

Note that the shortcut syntax for arrays can be helpful if you know the elements at the time of declaration. For example...

But if you're simply creating the array for elements to be added later, you can use the "new" syntax and just specify the array size. The elements will be initialized to null by default.

Also, "array" might not be the most descriptive name for your variable. You might consider calling it something like "playerNames" instead.
Jacob Coddaire
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

To access elements of an array, use the array name followed by the index enclosed in brackets. For example...

array[0] = "Shannon";
array[1] = "Tina";


The problem with this approach is I do not have definite names. So instead of that, I could put in the string variable, name.

So if i did

array[0] = name;
array[1] = name;
array[2] = name;
array[3] = name;

It would put one name for four "slots." Not four names.
How can I create a loop that assigns one name to one "slot" in the array?
Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1691
    
  12

hint: use the loop index

Jacob Coddaire
Ranch Hand

Joined: Jan 11, 2011
Posts: 36

So like this?

Janeice DelVecchio
Saloon Keeper

Joined: Sep 14, 2009
Posts: 1691
    
  12

Perfect
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 39436
    
  28
Please avoid long lines in code tags; they make the code difficult to read.
 
Don't get me started about those stupid light bulbs.
 
subject: How do you add objects to arrays?