Click here to Skip to main content
15,350,244 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
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];
    public E get(int index) {
        return list[index];
    public boolean add(E element) {
        return false;
    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 '<e>' 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)

1 solution

Good or not? It depends on what do you want to achieve. It looks like you have started it well, but the issue is: don't you think this is a bit of overkill? I mean, I cannot see what value do you add to the abstract class in your derived class, what new functionality. To me, it looks like using the available descendant generic class, ArrayList, would suffice:[^].

If you really need to add some custom functionality, please clarify. Also, I would understand if you do this just for learning purposes, but even in this case it would be good to set some goal which would require some custom functionality. Do you want an idea for such goal? I can suggest some; for example, you can implement some List-based interface through AbstractList-based implementation, which would allow you to read content from a big file with variable-size records. Just an example. If you don't have such goal, deriving your own class would be pointless.

Abbas A. Ali 10-Oct-14 16:10pm
@Sergey Although this started as an assignment from school but I turned it bigger in to a project for learning and addressing unaddressed issues in my programming in Java (Obviously I have not mastered the art). But since I have started implementing big. I want to complete it but more than that I want to learn on how to use Java Abstract classes that provide a way to implement data structure uniquely. I say uniquely because I was a c++ programmer and Java is a little different in implementing OO Concept.
The Goal was simply to create a data structure that best suited the need of my Chat Messenger. Obviously it is a small application which simply demonstrates the concept of Chatting, however I'd also like to know which structure is best suited for bigger applications too. This structure is only to hold online users for the time they are logged in so that I may use this information to send messages to the directed user.
Sergey Alexandrovich Kryukov 10-Oct-14 16:17pm
I did not get just one thing: what it that feature which would be missing from ArrayList but you would need?
Abbas A. Ali 10-Oct-14 16:26pm
@Sergey Like I said earlier I do not need any special features but for the sake of learning while searching internet I found Abstract classes and I couldn't find any useful tutorials on how to implement it. I got as far as I could on my own but its not far enough.
I used ArrayList but also would like to know how to accomplish same goals using the AbstractList.
Sergey Alexandrovich Kryukov 10-Oct-14 16:57pm
I understand it, appreciate your intentions.

You just need to implement the array-like functionality. Basically, you need to have some internal container of data. In particular, add(...) should add data to your internal data, it should increment size by one (when the size is checked up, it should be consistent), indexing of elements should return this data if the index is count at the moment of adding. And so on. You need the central point: some internal storage. Any questions about that?

You use E[] as the internal container. This is a bad container. The size of the array is immutable. You have either to allocate memory in advance (say, create 1024 or 1M elements), but it would be a waste of memory, or reallocate array every time. It would be better to create, say, some self-implemented linked list. Don't forget my idea about supporting of a big file.


This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900