Solved

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

Posted on 2006-07-18
8
271 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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:mayankeagle
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:
mayankeagle 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

Threat Intelligence Starter Resources

Integrating threat intelligence can be challenging, and not all companies are ready. These resources can help you build awareness and prepare for defense.

Join & Write a Comment

Suggested Solutions

For customizing the look of your lightweight component and making it look opaque like it was made of plastic.  This tip assumes your component to be of rectangular shape and completely opaque.   (CODE)
Java contains several comparison operators (e.g., <, <=, >, >=, ==, !=) that allow you to compare primitive values. However, these operators cannot be used to compare the contents of objects. Interface Comparable is used to allow objects of a cl…
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:
The viewer will learn how to implement Singleton Design Pattern in Java.

743 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now