Abbas
Abbas

Reputation: 3331

How to use AbstractList

I am newer to Java just started it a while back. I am trying to make chat messenger (Client-Server) and I need a data structure in which I can store online users (One socket and one String containing Username). Now I did a lot of research and thought AbstractList could help me decently, but I faced a problem rather quickly

public class Collections <E> extends AbstractList<E> {

    private int size;
    private E[] list;

    public Collections() {
        size = 0;
        list = new E[50];
    }

    @Override
    public E get(int index) {
        return list[index];
    }
    @Override
    public boolean add(E element) {


        return false;
    }

    @Override
    public int size() {
        return list.length;
    }
    public static void main(String[] args) {
        Collections <String>obj = new Collections();
        obj.add(new String("1"));
        obj.add(new String("5"));

        System.out.println("Size = " + obj.size());
    }
}

This is my code and frankly I couldn't get a way around the add function... I tried using

list = new E[50];

but that gives generic initialization error. In short I have no idea how to initialize the list object. So now I have Three questions:

  1. Is my approach to creating a list of all the clients on Server good?
  2. What should I do with the add() function?
  3. Why are there '' twice in the first line? Obviously it is a template but wouldn't you need it only once? Obviously you need it both places so what is the first one for and what is the second one for? (The Tutorial I learnt from didn't quiet explain it)

Upvotes: 0

Views: 2973

Answers (2)

Andy Hedges
Andy Hedges

Reputation: 301

To add items to a list you can do this, you don't need to create your own list class

List<String> myList = new ArrayList<String>();
myList.add("Peter");
myList.add("Susan");

Now if you want to create a list of all you username and sockets you could use a Map. Something like

Map<String, Socket> myMap = new HashMap<String, Socket>();
myMap.put("Peter", petersSocket);
myMap.put("Susan", susansSocket);

This will allow you to retrieve sockets by username

Socket petes = myMap.get("Peter");

Or you could instead create you own Username and Socket class

public class UsernameSocketPair{
  private String name;
  private Socket socket;
  public UsernameSocketPair(String name, Socket socket){
     this.name = name;
     this.socket = socket;
  }

  public String getName(){
     return name;
  }

  public Socket socket(){
     return socket;
  }
}

Then this can be put in a list like

List<UsernameSocketPair> myList = new ArrayList<UsernameSocketPair>();
myList.add(new UsernameSocketPair("Peter", petersSocket));

Whether this is a good idea or not, it depends on the rest of your code and how its structured.

To answer the questions explicitly:

  1. It depends on the rest of your code and how its structured.
  2. First don't use E[] use Object[] and cast to E when you 'get' the element. That way you can instantiate (e.g. Object[] list = new Object[20];
  3. The E is in the class declaration twice be cause you are extending a class of type AbstractList and you are parametrising your class by E. You could do, for example.

The following:

public class MyList extends AbstractList<String>{

} 

Below is a really noddy example of how to implement a concrete version of abstract list parametrised as you wanted

public class MyList <E> extends AbstractList<E> {

    private Object[] list = new Object[10];
    private int size = 0;


    public E get(int i){
        if(i >= size) throw new IndexOutOfBoundsException("duh!");
        return (E)list[i];
    }

    public boolean add(E e){
        if(size >= list.length){
            Object[] newList = new Object[list.length + 10];
            System.arraycopy(list,0, newList, 0, list.length);
            list = newList;
        }
        list[size] = e;
        size++;
        return true;
    }

    public int size(){
        return size;
    }

    public static void main(String[] args){
        List<String> l = new MyList<String>();
        for(int i = 0; i < 100; i++){
            l.add(""+i);
        }
        System.out.println(l.size());
    }

}

Upvotes: 2

Vivin Paliath
Vivin Paliath

Reputation: 95578

Your code has some design issues which I think stem from the fact that you are a beginner:

  • There is no need to extend AbstractList<E> unless you really require some special type of list. You can simply use a concrete implementation of List<T> like LinkedList<T> or ArrayList<T>.
  • Your code has a design problem. Your server is not a list. But this is what your code says, since your main class is extending AbstractList<E>. Instead, your server should be using a list. So the correct approach is to use an internal List<T> instance that keeps track of your clients.

Your approach is fundamentally wrong, so I would suggest reading up on some beginner texts on Java and Object-Oriented Programming in general. From your code it seems like you have some misconceptions as to what OOP actualy is.

Upvotes: 0

Related Questions