遍历标签图并处理它们

时间:2019-06-30 00:25:28

标签: java dictionary swt

我编写了以下代码:

''' SERVER CHAT CODE '''

import socket, select


port = 12345
socket_list = []
users = {}
# create an INET, STREAMing socket
server_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
# setting the value of the given socket
server_socket.setsockopt(socket.SOL_SOCKET, socket.SO_REUSEADDR, 1)
# associating the socket with a specific network interface and port 
number
server_socket.bind(('', port))
# enabling the server to accept connections
server_socket.listen(5)
socket_list.append(server_socket)

while True:
    ready_to_read, ready_to_write, in_error = select.select(socket_list, [], [], 0)
    for sock in ready_to_read:
        if sock == server_socket:
            connect, addr = server_socket.accept()
            socket_list.append(connect)
            # sending values from addr
            connect.sendto('You are connected from:' + str(addr), (port))
        else:
            try:
                # receive data from the socket
                data = sock.recv(2048)
                if data.startswith('#'):
                    users[data[1:].lower()] = connect
                    print('User ' + data[1:] + ' added.')
                    connect.send('Your user detail saved as: ' + str(data[1:]))
                elif datastartswith('@'):

users[data[1:data.index(':')].lower()].send(data[data.index(':') + 1:])
            except:
                continue

server_socket.close()



''' CLIENT CHAT CODE '''

import socket


client_socket = socket.socket()
port = 12345
client_socket.connect(('197.0.0.1', port))

# receive connection message from server
recv_msg = client_socket.recv(1024)
print(recv_msg)

# send user details to server
send_msg = input('Enter your username (prefix with #): ')
client_socket.sendto(send_msg, (port))

# receive and send message from/to different user/s
while True:
    recv_msg = client_socket.recv(1024)
    print(recv_msg)
    send_msg = input('Send your message in format [@user: message] ')
    if send_msg == 'exit':
        break
    else:
        client_socket.send(send_msg)

client_socket.close()

它会遍历标签图(类型:final Iterator<Entry<Label, Label>> it = dataLabels.entrySet().iterator(); while (it.hasNext()) { final Map.Entry<Label, Label> pairs = it.next(); dataLabels.remove(pairs.getKey()); pairs.getValue().dispose(); pairs.getKey().dispose(); } )并处理其中的每个标签(将其从地图中删除后)。

我觉得这段代码没有达到我的期望。我认为它在第一个循环后就停止了迭代(试图添加打印的消息以查看行为)。

代码是否符合我的期望(处理所有标签)?如果没有,该如何解决?

2 个答案:

答案 0 :(得分:0)

如果您要一一删除,请尝试

    Iterator<Map.Entry<Label,Label>> i = dataLabels.entrySet().iterator();
    while (i.hasNext()) {
       Map.Entry<String,String> entry = i.next();
       // Do something
       i.remove();
    }

否则,您可以使用dataLabels.clear()

清除整个地图

通过使用dataLabels.clear(),您可以重用dataLabels映射。

如果您不需要重复使用,请将其设置为null

dataLabels = null;

答案 1 :(得分:0)

遍历条目时,无法在地图上调用remove

您可以使用另一个答案中所示的迭代器for (Map.Entry<Label, Label> dataLabel : dataLabels.entrySet()) { dataLabel.getKey().dispose(); dataLabel.getValue().dispose(); } dataLabels.clear(); 方法,也可以只清除末尾的地图:

dataLabels.forEach((key, value) -> { key.dispose(); value.dispose(); });
dataLabels.clear();

或使用流:

reset_actions()