Reputation: 3331
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:
Upvotes: 0
Views: 2973
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:
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
Reputation: 95578
Your code has some design issues which I think stem from the fact that you are a beginner:
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>
.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