Results 1 to 4 of 4

Thread: java collection

  1. #1
    murugadoss is offline Member
    Join Date
    Jun 2013
    Rep Power

    Default java collection

    Hi let me share my problem, i hope its not something silly, so bear with me if i'm overlooking a detail that i should have seen.
    suppose we have two java file name is "" &"". having Vector title=new Vector(); List ls=new LinkedList(); both vector and list having some data .now i need to access(get that data) that both vector & list in how do i can?i tried but it's not coming...from the below program i got string but not vector and list.

    import java.util.Hashtable;
    import java.util.LinkedList;
    import java.util.List;
    import java.util.Vector;

    public class Second{

    Hashtable hash_table=new Hashtable();
    List l_value=new LinkedList();
    Vector v_title=new Vector();
    String title;
    public void table()
    First cc=new First();
    System.out.println("l_value :"+l_value);
    System.out.println("v_title :"+v_title);
    System.out.println("title :"+title);
    public static void main(String[] args) {

    Second cn=new Second();


  2. #2
    kalata is offline Member
    Join Date
    Aug 2011
    Rep Power

    Default Re: java collection

    Hi, these System.out.println-s will print only the value of the reference,i.e. the address of the collection in the memory, and you need to access the values via get(int index) method. This method will return you Object, so you need to cast it to the actual type of the element.
    If you are not familiar with Generics, it would be pretty useful for you to get to know them :) -> Lesson: Generics (Updated) (The Java™ Tutorials > Learning the Java Language)

    Last edited by kalata; 06-01-2013 at 05:30 PM.

  3. #3
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Northern Virginia, United States
    Rep Power

    Default Re: java collection

    What do you mean when you say its "not coming?" Can you show the output? Also, what does the First class look like. And as a side note, you should not use raw types when working with collections (or any code where you can apply generics). So if your List holds strings, it should be declared as:

    Java Code:
    List<String> ls = new LinkedList<>();  //java 1.7
                 // or 
    List<String> ls = new LinkedList<String>() // java 1.5+
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  4. #4
    DarrylBurke's Avatar
    DarrylBurke is offline Forum Police
    Join Date
    Sep 2008
    Madgaon, Goa, India
    Rep Power

Similar Threads

  1. collection class in java
    By katturv in forum New To Java
    Replies: 5
    Last Post: 10-24-2010, 07:09 PM
  2. java collection api
    By rajinder5 in forum Advanced Java
    Replies: 1
    Last Post: 10-07-2010, 01:51 PM
  3. java collection arraylist in jdk 1.4
    By jagdish_ in forum Advanced Java
    Replies: 1
    Last Post: 06-12-2010, 02:19 PM
  4. Collection - java
    By Moonlightkid in forum Advanced Java
    Replies: 3
    Last Post: 09-02-2008, 05:04 AM
  5. Java Collection Framework (Lists)
    By Java Tutorial in forum Java Tutorial
    Replies: 1
    Last Post: 05-16-2008, 01:44 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts