Results 1 to 4 of 4

Thread: java collection

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

    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 "First.java" &"Second.java". First.java 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 Second.java 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();
    l_value=(List)cc.ls;
    v_title=cc.title;
    title=cc.table_title;
    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();
    cn.table();

    }

  2. #2
    kalata is offline Member
    Join Date
    Aug 2011
    Location
    Bulgaria
    Posts
    29
    Rep Power
    0

    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)

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

  3. #3
    jim829 is online now Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    3,768
    Rep Power
    5

    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+
    Regards,
    Jim
    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 online now Forum Police
    Join Date
    Sep 2008
    Location
    Madgaon, Goa, India
    Posts
    11,405
    Rep Power
    20

    Default Re: java collection

    If you're forever cleaning cobwebs, it's time to get rid of the spiders.

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
  •