Search:

Type: Posts; User: javapenguin

Search: Search took 0.11 seconds.

  1. Thread: Generics

    by javapenguin
    Replies
    21
    Views
    2,605

    Re: Generics

    The best part, I think, is that your index number, say your list
    has
    [5, 3, 2, 1, 9]

    left would be

    index 0 = 5;
    index 1 = 3;
    index 2 =2;
    index 3 = 1;
  2. Thread: Generics

    by javapenguin
    Replies
    21
    Views
    2,605

    Re: Generics

    Well, add a method called toString() to your generic stack class.

    public String toString()
    {
    return list.toString();
    }

    then later, in your main class, you can

    stack.toString() to show a...
  3. Thread: Generics

    by javapenguin
    Replies
    21
    Views
    2,605

    Re: Generics

    import java.util.*;

    public class GenericStack<E>
    {
    private LinkedList<E> list = new LinkedList<E>();

    public void push(E item) //the parameter is an item of type E
    {
    ...
  4. Thread: Generics

    by javapenguin
    Replies
    21
    Views
    2,605

    Re: Generics

    For instance, if you want to get the top, usually the only place a Stack can get to,
    you'd be this:

    public E top()
    {
    return list.getFirst();
    }

    if you want to get an item at an index, you'd...
  5. Thread: Generics

    by javapenguin
    Replies
    21
    Views
    2,605

    Re: Generics

    Besides, if you're trying to get the item itself, your return type would be type E.

    Also, maybe this is just to show what it is, but I can't find this variable/class

    ...
  6. Thread: Generics

    by javapenguin
    Replies
    21
    Views
    2,605

    Re: Generics

    list.get(item) is invalid.

    List.contains(item)
    is valid

    list.get(int index) is valid.

    If you want the index, do a for loop and check for all the indexes to see if

    item =...
  7. Thread: Generics

    by javapenguin
    Replies
    21
    Views
    2,605

    Re: Generics

    The LinkedList class already has a get(int index) method.

    But again, a Stack usually is only concerned with get(0).

    The get method returns a variable of type E.

    If, using the for loop I gave...
  8. Thread: Generics

    by javapenguin
    Replies
    21
    Views
    2,605

    Re: Generics

    Well, hop through the list

    public int index (E item)
    {
    for (int i =0; i < size(); i++)
    {
    if (list.get(i).equals(item))
    return i;
    }
    return -1;
Results 1 to 8 of 8