0

I'm trying to implement list using arrays and generics. I'm stuck up on how to insert values into the generic list. Scanner's nextXXX variants expect a specific type but we know the type only at run time.

class Arraylist<T>
{
    static Scanner input = new Scanner(System.in);
    static T list[];
    static int top = -1;

    public static void displaymenu()
    {
        int choice;
        do {
            // get choice from user
            switch (choice) {
            case 1:
                list = createlist();
                break;
            case 2:
                insertnode();
                break;
            // ........
            }
        } while (true);
    }

    public static Object[] createlist()
    {
        list = new T[LIST_SIZE];
        return list;
    }

    public static void insertnode()
    {
        T o;
        top++;
        out.println("Enter the value to insert:");
        // o = user's input. I'm confused here???
    }
}

Thank you.

sinelaw
  • 15,049
  • 2
  • 44
  • 78
John
  • 9,897
  • 20
  • 63
  • 107
  • 1
    Can we have some indentation? – NullUserException Sep 12 '11 at 04:28
  • 1
    What are you trying to achieve here? You can't expect the user to input generic data, he has to know what type you're expecting (which is possible) and you have to know how to parse each type (which is only possible if you a way to parse every type, which you don't, unless you constrain the T to something that's always parsable). – sinelaw Sep 12 '11 at 04:33
  • Input is usually received as a String. From there, program logic can attempt to convert to some numerical value if need be. – Jonathan Weatherhead Sep 12 '11 at 04:56

1 Answers1

2

How about something like this:

public class ArrayList<T> {
    private T list[];
    private int last;
    public ArrayList() {
        list = (T[])new Object[10];
    }
    public void add(T elem) {
        if(last < list.length)
            list[last++] = elem;
        else {
            T newList[] = (T[])new Object[list.length*2];
            System.arraycopy(list, 0, newList, 0, list.length);
            list = newList;
            list[last++] = elem;
        }
    }
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append('[');
        for(int i = 0; i < last; i++) {
            sb.append(list[i].toString()+",");
        }
        sb.replace(sb.length()-1, sb.length(), "");
        sb.append(']');
        return sb.toString();
    }

    public static void main(String[] args) {
        ArrayList<String> stringList = new ArrayList<String>();
        stringList.add("Hello");
        stringList.add("World");
        stringList.add("Foo");
        System.out.println(stringList);
    }
}
blackcompe
  • 3,108
  • 14
  • 21
  • are Generics similar to C++ templates? I wonder why I cannot do T[] list = new T[10]; – John Sep 12 '11 at 11:33
  • John, Java has type erasure on generic types, see here: http://stackoverflow.com/questions/2927391/whats-the-reason-i-cant-create-generic-array-types-in-java and here: http://stackoverflow.com/questions/529085/java-how-to-generic-array-creation – sinelaw Sep 14 '11 at 11:41
  • 1
    "list[last++] = elem;" This line add to your else part. – Reegan Miranda Jul 28 '14 at 11:49
  • @Reegan: Thanks. I missed that. There's no reason in particular to create the initial size that big. It's just a number I chose. – blackcompe Jul 29 '14 at 14:04