how to get custom class from collection

Discussion in 'Java' started by column, Jun 19, 2008.

  1. column

    column Guest

    //have simple class:

    public class stud {
    public String getData() {...}
    ...............
    }


    //And I have class that contains many of stud in collection data


    public class studc {
    ArrayList data = new ArrayList();
    ...............

    //it is easy to add class in collection
    public void add(stud s)
    {
    data.add(s);
    }


    public String enumerate()
    {
    String s = new String();

    for (int i=0; i<data.size(); i++)
    {

    studentas f = data.get(i);
    // !!!! it is not possible to get stud type element from colection
    because arraylist returns
    //only Object (not stud class)

    s=s + f.getData()+"\n";
    }

    return s;
    }
    }


    How to get stud type object from data?
     
    column, Jun 19, 2008
    #1
    1. Advertising

  2. column

    Mark Guest

    On 19 Jun, 12:21, column <> wrote:
    > //have simple class:
    >
    > public class stud {
    > public String getData() {...}
    > ..............
    >
    > }
    >
    > //And I have class that contains many of stud in collection data
    >
    > public class studc {
    > ArrayList data = new ArrayList();
    > ..............
    >
    > //it is easy to add class in collection
    > public void add(stud s)
    > {
    > data.add(s);
    > }
    >
    > public String enumerate()
    > {
    > String s = new String();
    >
    > for (int i=0; i<data.size(); i++)
    > {
    >
    > studentas f = data.get(i);
    > // !!!! it is not possible to get stud type element from colection
    > because arraylist returns
    > //only Object (not stud class)
    >
    > s=s + f.getData()+"\n";
    > }
    >
    > return s;
    > }
    >
    > }
    >
    > How to get stud type object from data?


    Basic answer: Use a cast. "studentas f = (stud) data.get(i);"
    Advanced answer: Use stud as a type parameter: ArrayList<stud>
    Side issue: Always use a capital letter at the start of class names to
    distinguish them from variable names: Java doesn't care, but other
    Java programmers do.

    Mark
     
    Mark, Jun 19, 2008
    #2
    1. Advertising

  3. column

    Roedy Green Guest

    Roedy Green, Jun 19, 2008
    #3
    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. Dylan
    Replies:
    5
    Views:
    441
    Daniel T.
    Mar 22, 2005
  2. Pradeep
    Replies:
    2
    Views:
    692
    Patricia Shanahan
    Jan 24, 2007
  3. Øyvind Isaksen
    Replies:
    1
    Views:
    1,013
    Øyvind Isaksen
    May 18, 2007
  4. Terry Holland
    Replies:
    9
    Views:
    228
    Steven Cheng[MSFT]
    Oct 28, 2005
  5. Donald Simpson

    Web Custom Control & Custom Collection problem

    Donald Simpson, Jul 11, 2003, in forum: ASP .Net Web Controls
    Replies:
    0
    Views:
    139
    Donald Simpson
    Jul 11, 2003
Loading...

Share This Page