ShacoNotFound404
ShacoNotFound404

Reputation: 21

how to delete an element from an array of objects in java

in this program, i gather a nickname and an ip address for 50 different nicknames and IP addresses.

the ip address is separated into 4 integers (x.y.z.m) in it's class (which includes the nickname).

this method compares the x and y with the rest and records the nickname into a double string array. but i don't the same local network (same x and y) to be compared later in the loop so i want to delete each of the same x and y. how to do that?

Upvotes: 0

Views: 126

Answers (2)

eckes
eckes

Reputation: 10423

In your case you can just null the positions in the array you want to ignore (and make sure your code skips null entries). However more generally speaking arrays are not the best data structure to use.

I am not entirely sure what you want to do, it sounds like you want a list of all nicknames which have an IP Address in the same /16 network? In that case, I would use a MultiMap and use the prefix as the key. Then you simple need to put all addresses and later on get the collection for each of them.

Guava or Apache Commons Lang have such a structure:

Upvotes: 1

Code-Apprentice
Code-Apprentice

Reputation: 83527

Arrays are staticly sized, so you cannot easily remove an element from an array. I strongly suggest that you use a List type (such as ArrayList).

Upvotes: 0

Related Questions