Solved

How do I build an array of objects with a Java 5 for loop?

Posted on 2006-07-18
8
280 Views
Last Modified: 2010-03-31
How do I build an array of objects with a Java 5 for loop?

I'd like to populate an array simliar to:

private static final SelectItem[] stateItems = new SelectItem[] {
        new SelectItem("Nebraska"),
        new SelectItem("Texas"),
        new SelectItem("Kansas"),
        new SelectItem("Iowa"),
};

in a loop.

Each SelectItem object value comes from a data source that I can loop through with a Java 5 for statement.

However, what is the proper approach to populate the SelectItem[] stateItems array?

I guess, I get used to using ArrayList, which has an add() method.

Any suggestions or examples are appreciated.

Thank you,

--Todd

0
Comment
Question by:jtp51
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17135813
0
 
LVL 23

Expert Comment

by:Ajay-Singh
ID: 17135819
List<SelectItem> list = Arrays.asList(stateItems);
0
 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17135968
>> How do I build an array of objects with a Java 5 for loop?

Object[] obj = new Object[5];
for ( int i =  0 ; i <  array.length ; i ++  )
 obj[i] = new Object() ;



>>SelectItem object value comes from a data source that I can loop through with a Java 5 for statement

if you can get the data from the statement, then form a array of that value ( String array)

say, String[] data = { your values} ;

for ( int i = 0 ; i < stateitems.length ; i ++ )
   stateitems [ i]  = new SelectItem ( data[i] ) ;


if you are using arrayList then,

ArrayList stateitems = new ArrayList () ;
for ( int i = 0 ; i < data.length ; i ++  )

stateitems.add ( new SelectItem ( data [ i ] ) ) ;

0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 10

Expert Comment

by:mukundha_expert
ID: 17135974
>>private static final SelectItem[]

if the array is final then you cannot change its reference later, it should be initialzed once it is declared,
0
 
LVL 26

Expert Comment

by:ksivananth
ID: 17135983
SelectItem[] stateItems = new SelectedItem[ 5 ] ;

for( int i = 0; i < 5; i++ ) stateItems[i] = new SelectedItem() ;

Not sure if this is what u wnated ;)
0
 
LVL 30

Expert Comment

by:Mayank S
ID: 17136085
They are not Java 5 for loops. But anyway, why would you want to use the enhanced for loop? I personally don't prefer it as it doesn't give as good performance as the simple for loop.
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 17136804
Using new for loop would be something like

int ix = 0;
for (SelectItem si : dataSource) {
    stateItems[ix++] = si;
}
0
 
LVL 30

Accepted Solution

by:
Mayank S earned 50 total points
ID: 17138055
If you have them in an array-list/ collection, you can also directly use the toArray () method to get the values in an array.
0

Featured Post

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Configure a Bean in an XML file 4 49
type mismatch (Object[] to double[] 4 41
Convert from a json string array to a Java object 3 63
Java List 4 35
In this post we will learn different types of Android Layout and some basics of an Android App.
In this post we will learn how to make Android Gesture Tutorial and give different functionality whenever a user Touch or Scroll android screen.
Viewers will learn about if statements in Java and their use The if statement: The condition required to create an if statement: Variations of if statements: An example using if statements:
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:

756 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question