Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Java > how to get custom class from collection

Reply
Thread Tools

how to get custom class from collection

 
 
column
Guest
Posts: n/a
 
      06-19-2008
//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?



 
Reply With Quote
 
 
 
 
Mark
Guest
Posts: n/a
 
      06-19-2008
On 19 Jun, 12:21, column <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
 
Roedy Green
Guest
Posts: n/a
 
      06-19-2008
On Thu, 19 Jun 2008 04:21:28 -0700 (PDT), column
<(E-Mail Removed)> wrote, quoted or indirectly quoted someone
who said :

>// !!!! it is not possible to get stud type element from colection
>because arraylist returns
>//only Object (not stud class)


See http://mindprod.com/jgloss/generics.html

Otherwise you have to cast to (Studc) on each get.

Note the capital S. See
http://mindprod.com/jgloss/codingconventions.html
--

Roedy Green Canadian Mind Products
The Java Glossary
http://mindprod.com
 
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
Re: How include a large array? Edward A. Falk C Programming 1 04-04-2013 08:07 PM
Collection problems (create Collection object, add data to collection, bind collection to datagrid) Řyvind Isaksen ASP .Net 1 05-18-2007 09:24 AM
Update Custom Collection that is bound to DataGrid made up of Custom COlumns Terry Holland ASP .Net Datagrid Control 9 10-28-2005 06:20 AM
Nested Class, Member Class, Inner Class, Local Class, Anonymous Class E11 Java 1 10-12-2005 03:34 PM
Web Custom Control & Custom Collection problem Donald Simpson ASP .Net Web Controls 0 07-11-2003 07:52 AM



Advertisments