Query on handing array of object .

Discussion in 'Java' started by June Moore, Jan 7, 2004.

  1. June Moore

    June Moore Guest

    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...
     
    June Moore, Jan 7, 2004
    #1
    1. Advertising

  2. 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;
    > 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
     
    Christophe Vanfleteren, Jan 7, 2004
    #2
    1. Advertising

  3. "June Moore" <> wrote in message
    news:...
    ....
    | 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

    --
    Andrew Thompson
    * http://www.PhySci.org/ PhySci software suite
    * http://www.1point1C.org/ 1.1C - Superluminal!
    * http://www.AThompson.info/andrew/ personal site
     
    Andrew Thompson, Jan 7, 2004
    #3
  4. June Moore

    Adam Guest

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


    no need for casting to Student, ArrayList accepts Objects.

    Adam
     
    Adam, Jan 8, 2004
    #4
  5. June Moore

    Adam Guest

    "Adam" <> wrote in message
    news:btjr6f$hc9$...
    > > 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
     
    Adam, Jan 8, 2004
    #5
  6. June Moore

    Adam Guest

    "John C. Bollinger" <> wrote in message
    news:btn10f$gll$...
    > Adam wrote:
    >
    > > "Adam" <> wrote in message
    > > news:btjr6f$hc9$...
    > >
    > >>>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
     
    Adam, Jan 15, 2004
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. AC
    Replies:
    2
    Views:
    380
  2. =?Utf-8?B?Y2FzYXVib24=?=

    error handing

    =?Utf-8?B?Y2FzYXVib24=?=, Apr 28, 2005, in forum: ASP .Net
    Replies:
    1
    Views:
    375
    =?Utf-8?B?U2NjcjE4?=
    Jun 22, 2005
  3. Dips
    Replies:
    7
    Views:
    4,870
  4. Dave Merrill
    Replies:
    1
    Views:
    265
    Steven Bethard
    Jan 11, 2005
  5. Lie Ryan
    Replies:
    5
    Views:
    295
    Steven D'Aprano
    Nov 24, 2009
Loading...

Share This Page