tags:

views:

402

answers:

4
class Collection 
{ 
    int sNo; 
    String sessionID; 
    int noOfDependency; 
    int noOfRejection; 
    int totalValue;

    Collection(int sNo, String sessionID, int noOfDependency, int noOfRejection, int totalValue)
    {
        this.sNo = sNo;
        this.sessionID = sessionID;
        this.noOfDependency = noOfDependency;
        this.noOfRejection = noOfRejection;
        this.totalValue = totalValue;
    }
}

public class DependencyStack {

    /** Creates a new instance of DependencyStack */

    public DependencyStack() 
    {
        LinkedList lList = new LinkedList();
        lList.add(new Collection(1,"a",0,0,0);
        lList.add(new Collection(2,"b",0,0,0));

        for(int i=0;i<lList.size();i++);
            System.out.println(lList.getFirst());
    }

I am not able to view the individual data. For e.g. if i want to view all sno "serial nos.", how can i do that.. i have tried lot of options, please helpp...

A: 

Is there a reason you don't want to use a built in class, like List?

these classes take care of all the implementation details for you. look at java.util.List

Pete
List is an interface - no implementation details there.
Michael Borgwardt
+4  A: 

Use the standard API library as God, um, I mean Gosling intended.

Seriously, those have been tweaked, optimized and bugfixed a hundred times over - you're very unlikely to do any better.

In fact, java.util.LinkedList already has a size counter. But ArrayList is better for most cases (exception: if you often need to remove elements while traversing).

BTW, 500-1000 elements is chump change. It's nothing. You've wasted more time asking this question than your program would running an O(n) implementation a million times.

Edit: To store more than one piece of data in one node or list slot, write a class that has your data as fields (private and exposed via set and get methods if you want it to be particularly "clean") and put instances of that class into the list.

Michael Borgwardt
Well the information that i need to store contains multiple information under 1 node, is it possible with ArrayList..
AGeek
For e.g. sno,name,address,phoneNo.. All should be stored under 1 node. is it possible with array list, if yes, then just give me an idea from sample program.
AGeek
+1 for use of the word "chump"
banjollity
A: 

Java's LinkedList class keeps track of the size as you add/remove elements. Getting the size is free, you don't traverse the list. Don't worry about multiple pieces of info in a node, just put all you info in an object.

I know this is homework, but you really shouldn't seem so demanding and consider posting some code that illustrates you problem. We're not doing you work for you.

basszero
A: 
System.out.println(lList.getFirst());

lList.getFirst() returns an object of type Collection (you should use a better name BTW).

So you have to access to lList.getFirst().getSerialNumber() for example (and you also will have to write the method getSerialNumber() in your Collection class...

pgras
Given that the Collection class is in the same package and its properties not private, he doesn't necessarily have to use a get method (though of course it's cleaner).
Michael Borgwardt