爪哇 - 移除ArrayList中最后一个已知项 [英] Java - remove last known item from arraylist

查看:402
本文介绍了爪哇 - 移除ArrayList中最后一个已知项的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

行,所以这是我的ArrayList

OK so here is my arraylist.

private List<ClientThread> clients = new ArrayList<ClientThread>();

和这里就是我要做的。我想删除我上面贴的ArrayList中的最后一个已知的项目...这样做:

and here is what i am trying to do. I am trying to remove the last known item from the arraylist i posted above... doing this:

    } catch(SocketException re) {


                            String hey = clients.get(clients.size());
                            ClientThread.remove(hey);
                            System.out.println(hey + " has logged out.");
                            System.out.println("CONNECTED PLAYERS: " + clients.size());
}

但我得到这个错误:

but I am getting this error:

C:\wamp\www\mystikrpg\Server.java:147: incompatible types
found   : Server.ClientThread
required: java.lang.String
                        String hey = clients.get(clients.size());
                                                ^
C:\wamp\www\mystikrpg\Server.java:148: cannot find symbol
symbol  : method remove(java.lang.String)
location: class Server.ClientThread
                        ClientThread.remove(hey);
                                    ^
2 errors

我是什么做错了吗?
它假设从ArrayList中删除最后一个已知项目。

what am I doing wrong? it's suppose to remove the last known item from arraylist.

推荐答案

它应该是:

ClientThread hey = clients.get(clients.size() - 1);
clients.remove(hey);

或者你也可以做

clients.remove(clients.size() - 1);

的负的是因为大小()返回元件的数量,但ArrayList的第一个元素的索引为0,而不是1。

The minus ones are because size() returns the number of elements, but the ArrayList's first element's index is 0 and not 1.

这篇关于爪哇 - 移除ArrayList中最后一个已知项的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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