-3

I am currently writing a long class for an application I am working on. There is 2 lines of code that are constantly being repeated, however I cannot think of a quicker way to do it!

The lines are:

for (int row = 0; row < 8; row++)
    for (int col = 0; col < 8; col++)
        // Do Something

For such a simple "double loop" iteration, there must be some way of either reducing the number of times these two lines appear, or making the code simpler.

Can anyone reduce the character count of this?

EDIT:

For more information,

because the code is repeated so often, I either want to reduce the character count or use an alternative to two for-loops.

the code is used as following (for examples):

for (int row = 0; row < 8; row++)
    for (int col = 0; col < 8; col++)
        setVal();   

for (int row = 0; row < 8; row++)
    for (int col = 0; col < 8; col++)
        getVal(); 
ggle
  • 426
  • 3
  • 20
  • 5
    you are going to need to provide a lot more information in order for any of us to be of help. – geggleto Jan 07 '15 at 20:30
  • What is your exact goal? To reduce the character count (rename the variables), to reduce code duplication (create a helper method), or to improve efficiency (post and re-evaluate the algorithm used) – Kon Jan 07 '15 at 20:30
  • My best guess is Rhys wants to have C-like macros in Java. Whilst it would be pretty neat, it ain't possible, sorry. – MightyPork Jan 07 '15 at 20:31
  • 1
    The number of characters of those sentences have **little relation** with its efficiency. – Christian Tapia Jan 07 '15 at 20:34
  • Take a look at the reflection in Java. Maybe [this](http://stackoverflow.com/questions/37628/what-is-reflection-and-why-is-it-useful) will help? Put those loops inside a static method to which you will pass the reference to an `Object` and method name. Then use reflection to invoke the method. – Jagger Jan 07 '15 at 20:35
  • Reflection, this is new to me, but looks like its exactly what I need... – ggle Jan 07 '15 at 20:44
  • @Jagger A lambda or other functional interface seems to be a better choice than using reflection. – ajb Jan 07 '15 at 20:55
  • @ajb You are totally right. – Jagger Jan 07 '15 at 21:00

3 Answers3

1

I don't know what's wrong with the nested loops, but you can make this a bit shorter in java 8 using lambdas.

Write a general method representing the nested loops.

private static void loops(BiConsumer<Integer, Integer> biConsumer) {
    for (int row = 0; row < 8; row++)
        for (int col = 0; col < 8; col++)
            biConsumer.accept(row, col);
}

You can then call it like this:

loops((row, col) -> System.out.println("The cell is (" + row + ", " + col + ")"));

In general you can write it like this:

loops((row, col) -> {
        // Put your code in here.
});
Paul Boddington
  • 37,127
  • 10
  • 65
  • 116
1

Assuming you want setVal() and getVal() to know about row and column you could do something like the following:

void matrixExecute(MatrixExecutor executor) {
    for (int row = 0; row < 8; row++) {
        for (int col = 0; col < 8; col++) {
            executor.execute(col, row);
        }
    }
}
private interface MatrixExecutor {
    void execute(int col, int row);
}

To use this implement inline:

matrixExecute(new MatrixExecutor() {
    @Override
    public void execute(int col, int row) {
        System.out.println("col:" + col + "row:" + row);
        }
    });

While this is still pretty wordy, when you move to Java 8 and the lamda expressions it's pretty nice:

matrixExecute((col,row) -> { System.out.println("col:"+col+"row:"+row);});

Note that will need to make sure you know about closures.

JimW
  • 186
  • 8
0

Well, there's this formulation.

    for (int i = 0; i < 64; i++) {
        int row = i / 8;
        int col = i % 8;
        // do something
    }

Although I don't think that's what you're looking for.

Gilbert Le Blanc
  • 50,182
  • 6
  • 67
  • 111