Java – 从ArrayList中删除最后一个已知项
好的,所以这是我的ArrayList
:
private List clients = new ArrayList();
这就是我要做的事情:
我试图从上面发布的ArrayList
删除最后一个已知项。 我正在尝试使用以下代码执行此操作:
} 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()); }
但是我收到了这个错误:
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
删除最后一个已知项。
它应该是:
ClientThread hey = clients.get(clients.size() - 1); clients.remove(hey);
或者你可以做到
clients.remove(clients.size() - 1);
减号是因为size()返回元素的数量,但ArrayList的第一个元素的索引是0而不是1。
编译器抱怨您正在尝试将一些ClientThread
对象列入String
。 将hey
的类型更改为ClientThread
或将clients
更改为List
。
另外:列表的有效索引从0到size() – 1。
所以你可能想写
String hey = clients.get(clients.size()-1);
此行表示您实例化了“ClientThread对象列表”。
private List clients = new ArrayList ();
这条线有两个问题。
String hey = clients.get(clients.size());
1.这部分内容:
clients.get(clients.size());
总是抛出IndexOutOfBoundsException,因为集合大小总是比最后一个元素索引大一个;
2.编译器抱怨不兼容的类型,因为您无法将ClientThread对象分配给String对象。 正确的应该是这样的。
ClientThread hey = clients.get(clients.size()-1);
最后但并非最不重要的。 如果你知道要删除的对象的索引只是写
clients.remove(23); //Lets say it is in 23. index
不要写
ClientThread hey = clients.get(23); clients.remove(hey);
因为您强制列表搜索您已经知道的索引。 如果您打算稍后对删除的对象执行某些操作。 写
ClientThread hey = clients.remove(23);
这样,您可以删除对象并在同一行获取对它的引用。
额外奖励:永远不要使用名称“嘿”调用您的实例变量。 找到有意义的东西
以下是您已更正且可立即运行的代码:
public class ListExampleForDan { private List clients = new ArrayList (); public static void main(String args[]) { clients.add(new ClientThread("First and Last Client Thread")); boolean success = removeLastElement(clients); if (success) { System.out.println("Last Element Removed."); } else { System.out.println("List Is Null/Empty, Operation Failed."); } } public static boolean removeLastElement(List clients) { if (clients == null || clients.isEmpty()) { return false; } else { clients.remove(clients.size() - 1); return true; } } }
请享用!
您正在尝试将clients.get(clients.size())
的返回值分配给字符串hey
,但返回的对象是ClientThread
,而不是字符串。 正如安德烈提到的 ,你也需要使用正确的索引。
就您的第二个错误而言, ClientThread
类型上没有静态方法remove()
。 实际上,您可能想要List
实例的删除方法, clients
。
您可以从列表中删除最后一项(如果有),如下所示。 由于remove还会返回已删除的对象,因此您可以捕获返回并使用它来打印出名称:
int size = clients.size(); if (size > 0) { ClientThread client = clients.remove(size - 1); System.out.println(client + " has logged out."); System.out.println("CONNECTED PLAYERS: " + clients.size()); }
clients.get
将返回一个ClientThread
而不是一个String
,如果它因为Java为零而编译,它将使用IndexOutOfBoundsException
编译。
同样地,我认为你应该在clients
列表上调用remove
。
ClientThread hey = clients.get(clients.size()-1); clients.remove(hey); System.out.println(hey + " has logged out."); System.out.println("CONNECTED PLAYERS: " + clients.size());
在这种情况下,我会使用LinkedList
的堆栈函数。
ClientThread hey = clients.removeLast()
第一个错误:由于某种原因,您正在将ClientThread
为String
。
第二个错误:您没有在List
调用remove
。
是作业吗? 如果是这样,您可能想要使用该标记。
你需要了解javagenerics 。 你有一个ClientThread
列表但是试图获取String
。 你有其他错误,但这个是非常基本的。