所以我正在测试运营商,因为我用java帮助我的朋友,我偶然发现了一个奇怪的编程顺序.运行以下代码时发生了什么
public static void main(String[] args) {
int B = 6;
//First console print out
System.out.println(B+=++B);
System.out.println(B);
B = 6;
//Second Console print out
System.out.println(B+=B++);
System.out.println(B);
}
Run Code Online (Sandbox Code Playgroud)
以下代码的输出是
13
13
12
12
Run Code Online (Sandbox Code Playgroud)
是什么原因导致第二个控制台B数学输出= 12时它自己加6,然后是++(这是+1)
我正在运行一个服务器,我有一个类型为Client的arraylist当客户端通过ServerSocket.accept()连接时,我将新的Socket传递给arraylists构造函数.这是构造函数中的所有内容
this.add(new Client(Socket client));
Run Code Online (Sandbox Code Playgroud)
我的问题是当客户端断开连接时,它会关闭Socket,但它不会删除它在arraylist中的位置,并将所有内容都移到一个位置.所以arraylist不断变大.
我可以做什么/用来解决这个问题?
有时我会运行将在所有客户端上执行的命令,这就是我将客户端存储在arraylist中的原因.
是否有更好的替代方法在服务器中存储客户端?
更新1
课程处于起步阶段.很少实施.到目前为止,答案中建议的HashMap选项最适合我.谢谢您的回复
我是第一次尝试与布局管理人员合作,他们只是在踢我.我正在尝试制作背景图像,然后使用GridBagLayout将按钮放在顶部,如果有更好的布局管理器请告诉我.至于尝试学习如何使用布局管理器,其非常困难和任何学习参考也将非常感激.
这就是它目前的样子,我可以得到框架来显示完整的图像,但是当我使用gridlayout管理器时,它会这样做
public void addComponentsToPane(Container pane){
BackgroundImage image = new BackgroundImage();
JButton button1, button2, button3, button4, button5;
pane.setLayout(new GridBagLayout());
GridBagConstraints c = new GridBagConstraints();
if(shouldFill){
c.fill = GridBagConstraints.NONE;
}
button1 = new JButton("Button 1");
if (shouldWeightX) {
c.weightx = 0.5;
}
c.fill = GridBagConstraints.HORIZONTAL;
c.gridx = 1;
c.gridy = 0;
button1.setOpaque(false);
pane.add(button1, c);
button2 = new JButton("Button 2");
c.fill = GridBagConstraints.HORIZONTAL;
c.weightx = 0.5;
c.gridx = 0;
c.gridy = 0;
button2.setOpaque(false);
pane.add(button2, c);
button3 = new JButton("Button 3");
c.fill = …
Run Code Online (Sandbox Code Playgroud)