ArrayList只列出一个Java条目
我有一个arraylist,每次运行我的客户端时,我都会尝试添加一些新的播放器,但我只添加一个播放器。我不确定我的arraylist和id是否是公共的,以及如何在我运行的每个线程中使用它们ArrayList只列出一个Java条目,java,arrays,list,Java,Arrays,List,我有一个arraylist,每次运行我的客户端时,我都会尝试添加一些新的播放器,但我只添加一个播放器。我不确定我的arraylist和id是否是公共的,以及如何在我运行的每个线程中使用它们 import java.io.DataInputStream; import java.io.DataOutputStream; import java.io.IOException; import java.net.ServerSocket; import java.net.Socket; import j
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.ArrayList;
import java.util.List;
public class server {
@SuppressWarnings("resource")
public static void main(String[] args) throws IOException {
ServerSocket socketS = null;
try {
socketS = new ServerSocket(4444);
} catch (IOException e) {
System.err.println("Could not listen on port: 4444.");
System.exit(-1);
}
while (true)
new ServerClass(socketS.accept()).start();
}
}
class ServerClass extends Thread {
public int id = 0;
Socket s;
game g;
private boolean ft = true;
DataInputStream in;
DataOutputStream out;
private int currid2;
private int currid1;
private String name1, name2;
List<player> players = new ArrayList<player>();
public ServerClass(Socket s) {
super("NewPlayer");
players.add(id, new player(id, s));
System.out.println(players.size() + " " + id);
this.s = s;
id++;
}
public void run() {
try {
in = new DataInputStream(s.getInputStream());
out = new DataOutputStream(s.getOutputStream());
} catch (Exception e) {
e.printStackTrace();
}
if ((id % 2) != 0) {
while ((id % 2) != 0) {
try {
Thread.sleep(10);
} catch (InterruptedException e1) {
e1.printStackTrace();
}
}
while (true) {
try {
String message = null;
while (message == null) {
message = in.readUTF();
}
if (ft != true) {
String x = Thread.currentThread().getName();
System.out.println(" " + message + " \n" + players.get(id).getID() + " " + players.get(id).getS());
System.out.println("Thread name: " + x);
out.writeUTF("over");
} else {
out.writeUTF("Give me your name");
name2 = in.readUTF();
currid2 = g.getID2();
players.get(currid2).setName(name2);
ft = false;
}
} catch (IOException e) {
e.printStackTrace();
}
}
} else {
while (true) {
try {
//g = new game(players.get(0), players.get(1));
String message = null;
while (message == null) {
message = in.readUTF();
}
if (ft != true) {
String x = Thread.currentThread().getName();
System.out.println(" " + message + " \n" + players.get(id).getID() + " " + players.get(id).getS());
System.out.println("Thread name: " + x);
out.writeUTF("over");
} else {
out.writeUTF("Give me your name");
name2 = in.readUTF();
currid2 = g.getID2();
players.get(currid2).setName(name2);
}
} catch (IOException e) {
in = null;
out = null;
}
}
}
}
}
import java.io.DataInputStream;
导入java.io.DataOutputStream;
导入java.io.IOException;
导入java.net.ServerSocket;
导入java.net.Socket;
导入java.util.ArrayList;
导入java.util.List;
公共类服务器{
@抑制警告(“资源”)
公共静态void main(字符串[]args)引发IOException{
ServerSocket=null;
试一试{
套接字=新服务器套接字(4444);
}捕获(IOE异常){
System.err.println(“无法侦听端口:4444”);
系统退出(-1);
}
while(true)
新的ServerClass(socketS.accept()).start();
}
}
类ServerClass扩展线程{
公共int id=0;
插座;
游戏g;
私有布尔ft=true;
数据输入流输入;
数据输出流输出;
私人国际货币2;
私人国际货币1;
私有字符串name1,name2;
List players=new ArrayList();
公共服务器类(套接字){
超级(“新玩家”);
玩家。添加(id,新玩家(id,s));
System.out.println(players.size()+“”+id);
这个.s=s;
id++;
}
公开募捐{
试一试{
in=新的DataInputStream(s.getInputStream());
out=新的DataOutputStream(s.getOutputStream());
}捕获(例外e){
e、 printStackTrace();
}
如果((id%2)!=0){
而((id%2)!=0){
试一试{
睡眠(10);
}捕捉(中断异常e1){
e1.printStackTrace();
}
}
while(true){
试一试{
字符串消息=null;
while(message==null){
message=in.readUTF();
}
如果(ft!=真){
字符串x=Thread.currentThread().getName();
System.out.println(“+message+”\n“+players.get(id).getID()+”+players.get(id).getS());
System.out.println(“线程名称:+x”);
注销(“超过”);
}否则{
把你的名字告诉我;
name2=in.readUTF();
currid2=g.getID2();
players.get(currid2).setName(name2);
ft=假;
}
}捕获(IOE异常){
e、 printStackTrace();
}
}
}否则{
while(true){
试一试{
//g=新游戏(players.get(0),players.get(1));
字符串消息=null;
while(message==null){
message=in.readUTF();
}
如果(ft!=真){
字符串x=Thread.currentThread().getName();
System.out.println(“+message+”\n“+players.get(id).getID()+”+players.get(id).getS());
System.out.println(“线程名称:+x”);
注销(“超过”);
}否则{
把你的名字告诉我;
name2=in.readUTF();
currid2=g.getID2();
players.get(currid2).setName(name2);
}
}捕获(IOE异常){
in=null;
out=null;
}
}
}
}
}
您制作了一个包含“玩家”的ArrayList,因此您需要向列表中添加一个玩家,如下所示:
添加(新玩家(id,s));
但我没有看到任何球员级别。顺便说一下,类应该总是以大写字母开头。
你的代码很漂亮。。。搞混了
我希望我的答案是正确的,哈哈。要将玩家添加到ArrayList,需要调用List.add方法。您已经在ServerClass构造函数中这样做过一次。您在此行只添加了一个玩家
players.add(id,newplayer(id,s))代码>
您需要添加更多类似这些行的内容。例如:
players.add(new player(++id, s));
players.add(new player(++id, s));
players.add(new player(++id, s));
这些行将在您的列表中添加另外3名玩家。不过,您需要编写为玩家创建唯一ID的逻辑,并确定何时需要添加新玩家
编辑:如果每次连接客户端时都需要添加新播放器,并且根据我从代码中了解的情况,“ServerClass”是处理客户端的类。然后需要在服务器类之间共享ArrayList。目前,每个ServerClass对象都将创建一个新的ArrayList(我们不希望这样)
将玩家列表添加到游戏对象中怎么样?我想游戏对象是在所有客户端之间共享的
public interface PlayerProviderable
{
Player createNewPlayer(Socket socket);
Player getPlayer(int key); // maybe the key is an index...depending on implementation
}
public class Game implements PlayerProviderable
{
// fields as usual...
// constructor as usual
// add a List for the player here...or a map.
// Map<Integer, Player> players = new HashMap<Integer, Player>();
List<Player> players = new ArrayList<Player>(); // if this is the list implementation. consider using a Map instead.
public synchronized Player createNewPlayer(Socket socket)
{
// add the player to a list or to a dictionary.
// this is the List implementation.
int id = players.size();
players.add(new Player(id, socket));
//players.put(id, new Player(id, socket)); // to a map.
}
public synchronized Player getPlayer(int key)
{
// retrieve a player from a list or from a dictionary with the given key. the key is the id or the index. You decide.
// it could be better to use a Map here instead of a list.
// but this is the a simple list implementation; Will throw an exception or return null (implementation specific) if the key does not exists.
return players.get(key); // as we did not provide a method to delete a player. Otherwise, you will need to iterate over all of the players and find the one with the id you wanted.
// or just use a Map instead of a list.
}
}
如果你不能缩进你的代码,你甚至不应该考虑使用套接字和线程。这是我大学的练习,练习不应该被正确缩进?你的意思是我发布代码的方式是错误的?你在每个衍生线程(服务器类)中创建一个玩家列表,试图避免这种情况,并且只使用一个列表(可能在你的服务器类中保存它并将其传递给服务器类)还要注意你的列表是线程安全的(synchronizedList)我有类播放器,但我没有发布它,请尝试:players.add(新播放器(数字,字符串));我也尝试过这个方法。最后我将列表更改为静态,现在可以正常工作。我想在每次客户端运行时添加一个播放器,我不能使用播放器。添加(id,新播放器(id,s));并向我的列表中添加点头?所以您想在每次收到消息时添加一个播放器?在'message=in.readUTF()中;
public class ServerClass extends Thread
{
// all the fields you need. minus the List<Player>.
public ServerClass(Game game, Socket s)
{
super("NewPlayer");
g = game; // g is the name that you used. consider changing it to a better name.
// continue initialization as normal.
// you do not need the key
game.createNewPlayer(s);
}
// at the run method.
public void run()
{
//start as usual...
else
{
out.writeUTF("Give me your name");
name2 = in.readUTF();
currid2 = g.getID2();
Player player = game.getPlayer(currid2);
if(null != player)
{
player.setName(name2);
}
ft = false;
}
// continue as usual...
}
}
public class Server
{
public static void main(String ... args) throws IOException // It is better to catch it here...otherwise your app will be terminated.
{
// as usual...
Game game = new Game( ... ); // create your game here.
while(true) // consider to use a flag or other logic for stopping.
new ServerClass(game, socketS.accept()).Start();
// as usual...
}