S
S
SankaSanka2021-11-13 22:35:30
Java
SankaSanka, 2021-11-13 22:35:30

What is the best way to iterate through all possible combinations of 4 arrays?

I'm trying to write a small Java program that will generate absolutely all possible variants of IP addresses.
from 0.0.0.0 to 255.255.255.255 there is no need to check for reality. just sort through the numbers.
I tried to save to files

using for loops, but it turns out some kind of wild spaghetti with an uncontrolled result.

Can you please tell me what is the best way to do this? Maybe there are some libraries for such cases?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
B
BorLaze, 2021-11-13
@SankaSanka

Hmm...what's wrong with a simple cycle with 4 levels, if you need "absolutely all possible variants of IP addresses"? At the same time, the addresses will go in order ...

for(int i0=0; i<=255; i++) {
    for(int i1=0; i<=255; i++) {
        for(int i2=0; i<=255; i++) {
            for(int i3=0; i<=255; i++) {
                writeToFile(printf("%d.%d.%d.%d\n", i0, i1, i2, i3));
            }
        }
    }
}

R
rPman, 2021-11-13
@rPman

ip4 address is a 32bit number, all ip addresses here are all unsigned int32 values ​​from 0 to 0xFFFFFFFF
i.e. you write one single cycle from 0 to 0xFFFFFFFF and then from the number you get ip in the form you need, either by sequentially obtaining the remainder of the branch by 256, or manually by bitwise operations (mask and shift) or by converting the number into an array of bytes

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question