Solved

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

Posted on 2006-07-18
8
284 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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
Independent Software Vendors: 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: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone 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

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…
Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

731 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