Query on handing array of object .

J

June Moore

Hi there,

I have a query regarding array of object.
For example, I have a class Student. I create an array of Student --

private Student[] students;

I also have an ArrayList --

private ArrayList studentList;

From the above example,
How do I get each Student object instance from the students array and
add each Student object to the studentList?

Can I do this?

int siz = students.size(); // not sure if this is correct?
for (int i=0; i < siz; i++)
{
Student student = students;
studentList.add(student);
}

Is this correct? If so, is this efficient? OR can I just CAST students
as an ArrayList without going through the looping?

Thanks,
June...
 
C

Christophe Vanfleteren

June said:
Hi there,

I have a query regarding array of object.
For example, I have a class Student. I create an array of Student --

private Student[] students;

I also have an ArrayList --

private ArrayList studentList;

From the above example,
How do I get each Student object instance from the students array and
add each Student object to the studentList?

Can I do this?

int siz = students.size(); // not sure if this is correct?
for (int i=0; i < siz; i++)
{
Student student = students;
studentList.add(student);
}

Is this correct? If so, is this efficient? OR can I just CAST students
as an ArrayList without going through the looping?


Yes you can do it that way.
But look at java.util.Arrays.asList(Object[]), which does this for you.
 
A

Andrew Thompson

....
| private Student[] students;
....
| private ArrayList studentList;
...
| int siz = students.size(); // not sure if this is correct?

no. arrays have an attribute called length..
http://java.sun.com/docs/books/tutorial/java/data/arrays.html

| for (int i=0; i < siz; i++)
| {
| Student student = students;
| studentList.add(student);
| }
|
| Is this correct? If so, is this efficient?

try this..
for (int i=0; i < students.length; i++)
{
studentList.add((Student)students);
}

You might consider directing these level of
questions to comp.lang.java.help

HTH
 
A

Adam

try this..
for (int i=0; i < students.length; i++)
{
studentList.add((Student)students);
}


no need for casting to Student, ArrayList accepts Objects.

Adam
 
A

Adam

Adam said:
try this..
for (int i=0; i < students.length; i++)
{
studentList.add((Student)students);
}


no need for casting to Student, ArrayList accepts Objects.


Brr...
And 'students' is of type Student[], so i don't get the idea of this cast at
all.

Adam
 
A

Adam

John C. Bollinger said:
Adam said:
try this..
for (int i=0; i < students.length; i++)
{
studentList.add((Student)students);
}

no need for casting to Student, ArrayList accepts Objects.



Brr...
And 'students' is of type Student[], so i don't get the idea of this cast at
all.


The cast applies to the array element, not the array.

Yes, I'm aware of that. The question is: what for?
Student[] students;
.... = (Student)students;
is absolutely unnecessary, you can only have Student objects in the array.
(Or subclasses of Student).
Anyway List.add accepts Objects, so anything you add will be
cast to Object first.
Why confuse people?
To get the other
behavior you need to use parentheses:
Hmm... what other behaviour?
((Student[]) students)

Kind of ugly, but it works.

And should be avoided as often as possible,
like all casting.

Adam
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,536
Members
45,011
Latest member
AjaUqq1950

Latest Threads

Top