如何在Java中使用集合 [英] How to Use Collection in Java

查看:104
本文介绍了如何在Java中使用集合的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述



我刚刚开始使用Java了。我正在尝试使用聊天信使(Client-Server),我需要一个数据结构,我可以在其中存储在线用户(一个套接字和一个包含用户名的字符串)。现在我做了很多研究,并认为AbstractList可以帮助我体面,但我很快就遇到了问题

Hi,
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];



但这会产生通用初始化错误。简而言之,我不知道如何初始化列表对象。

所以现在我有三个问题:

1.我的方法是创建所有客户的列表服务器好吗?

2.我应该用add()函数做什么?

3.为什么第一行有'< e>'两次?显然它是一个模板,但你不需要它只一次吗?显然你需要两个地方所以第一个是什么,第二个是什么? (我从中学到的教程并不安静解释)


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)

推荐答案

好还是不好?这取决于你想要达到的目标。看起来你已经开始做得很好,但问题是:你不觉得这有点过分吗?我的意思是,我看不出你在派生类的抽象类中添加了什么值,有什么新功能。对我来说,看起来使用可用的后代泛型类 ArrayList 就足够了:

http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html [ ^ ] 。



如果你真的需要添加一些自定义功能,请澄清。另外,如果你这样做只是出于学习目的,我会理解,但即使在这种情况下,设置一些需要一些自定义功能的目标也是一件好事。你想要这个目标的想法吗?我可以建议一些;例如,您可以通过基于 AbstractList 的实现实现一些基于 List 的接口,这将允许您阅读内容来自具有可变大小记录的大文件。只是一个例子。如果你没有这样的目标,那么推导自己的课程将毫无意义。



-SA
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:
http://docs.oracle.com/javase/8/docs/api/java/util/ArrayList.html[^].

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.

—SA


这篇关于如何在Java中使用集合的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆