Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > Query on handing array of object .

Reply
Thread Tools

Query on handing array of object .

 
 
June Moore
Guest
Posts: n/a
 
      01-07-2004
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[i];
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...
 
Reply With Quote
 
 
 
 
Christophe Vanfleteren
Guest
Posts: n/a
 
      01-07-2004
June Moore wrote:

> 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[i];
> 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.



--
Kind regards,
Christophe Vanfleteren
 
Reply With Quote
 
 
 
 
Andrew Thompson
Guest
Posts: n/a
 
      01-07-2004
"June Moore" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) om...
....
| 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/tutor...ta/arrays.html

| for (int i=0; i < siz; i++)
| {
| Student student = students[i];
| 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[i]);
}

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

HTH

--
Andrew Thompson
* http://www.PhySci.org/ PhySci software suite
* http://www.1point1C.org/ 1.1C - Superluminal!
* http://www.AThompson.info/andrew/ personal site


 
Reply With Quote
 
Adam
Guest
Posts: n/a
 
      01-08-2004
> try this..
> for (int i=0; i < students.length; i++)
> {
> studentList.add((Student)students[i]);
> }


no need for casting to Student, ArrayList accepts Objects.

Adam


 
Reply With Quote
 
Adam
Guest
Posts: n/a
 
      01-08-2004

"Adam" <(E-Mail Removed)> wrote in message
news:btjr6f$hc9$(E-Mail Removed)...
> > try this..
> > for (int i=0; i < students.length; i++)
> > {
> > studentList.add((Student)students[i]);
> > }

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


 
Reply With Quote
 
Adam
Guest
Posts: n/a
 
      01-15-2004

"John C. Bollinger" <(E-Mail Removed)> wrote in message
news:btn10f$gll$(E-Mail Removed)...
> Adam wrote:
>
> > "Adam" <(E-Mail Removed)> wrote in message
> > news:btjr6f$hc9$(E-Mail Removed)...
> >
> >>>try this..
> >>>for (int i=0; i < students.length; i++)
> >>>{
> >>> studentList.add((Student)students[i]);
> >>>}
> >>
> >>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[i];
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)[i]
>
> Kind of ugly, but it works.

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

Adam


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
PIX DCHPD handing out multiple IPs to single MAC gord.veri@gmail.com Cisco 3 03-02-2006 06:03 PM
French Gov handing out Linux in schools Imhotep Computer Security 20 08-18-2005 11:26 PM
error handing =?Utf-8?B?Y2FzYXVib24=?= ASP .Net 1 06-22-2005 07:52 PM
API for Handing Word Document Dips Java 7 05-20-2005 03:45 PM
Issues with handing server events... AC ASP .Net 2 12-19-2003 02:08 PM



Advertisments