Java: check whether the IP address is in the array list
•
Java
Is there a special method to check whether there are IP addresses in ArrayList? Now I have an ArrayList composed of IP address strings (e.g. "192.168.0.4", etc.) After receiving the packet, I want to check whether the IP address of the packet belongs to ArrayList
At first I thought such things were enough:
for (int i = 0; i < myList.size(); i++)
{
if (packet.getAddress().equals(InetAddress.getByName(myList.get(i))))
{
System.out.println("this packet's IP address is in list");
}
else
{
System.out.println("this packet's IP address is not in list!");
}
I thought other statements would solve the situation, but I was wrong Any suggestions would be appreciated
Solution
Before you know that the IP does not exist, you must check the entire list:
boolean found = false;
for (int i = 0; i < myList.size() && !found; i++) {
if (packet.getAddress().equals(InetAddress.getByName(myList.get(i)))) {
found = true;
System.out.println("this packet's IP address is in list");
}
}
if (!found) {
System.out.println("this packet's IP address is not in list!");
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
二维码
