Solved

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

Posted on 2006-07-18
8
274 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Running Jira on Raspberry PI 2? 3 264
@SBGen Method 3 38
difference of if loops 23 41
factorial example 4 35
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Viewers will learn about basic arrays, how to declare them, and how to use them. Introduction and definition: Declare an array and cover the syntax of declaring them: Initialize every index in the created array: Example/Features of a basic arr…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

920 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