HiveBrain v1.2.0
Get Started
← Back to all entries
patternjavaMinor

Working with list in Java

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
withlistworkingjava

Problem

This code has a method that I call for in main.

I am given a list with temperatures. What I need to do is to write out the biggest absolute difference of 2 neighbour numbers. For example, if I have {1, 3, 4, 10, 7, 4}, the result is 6 (between 4 and 10) and also all other pairs that have the same difference.

I have this code written down but I feel it looks kind of clumsy. I'd be very grateful for any suggestions and improvements.

public class DN4 {
    static void najvecjaZaporednaRazlika(double stevila[]){
        double maxRaz = 0;
        double[] tStevil = new double[0];
        for (int i = 0; i  maxRaz){
                maxRaz = absRaz;
                tStevil = new double[2];
                tStevil[0] = a;
                tStevil[1] = b;
            }
        }
        System.out.printf("Najvecja razlika: %.2f\n",maxRaz);
        System.out.printf("Pari,ki dosezejo maksimalno razliko: ");
        for (int i = 0; i < tStevil.length - 1; i = i+2){
            if (i == 0){
                System.out.printf("(%.2f, %.2f)",tStevil[i],tStevil[i+1]);
            }
            else{
                System.out.printf(", (%.2f, %.2f)",tStevil[i],tStevil[i+1]);
            }
        }
        System.out.println();
    } 

    public static void main (String args[]){
        double kredarica2011[] = {-7.8, -6.5, -5.7, -1.3, 1.7,  5,
                               5.6,  9.2,  7.1,  0.5, 0.3, -6.3};
        najvecjaZaporednaRazlika(kredarica2011);
    }  
}

Solution

General

-
Your code style is neat. I like that you are using System.out.printf instead of concatenating strings. You should consider using %n instead of \nin the format because it is platform independent.

-
Math.abs() - Math.abs() is a core function that you should use instead of doing it yourself.

-
Growing the tStevil array - you do this manually, and you increase and decrease the size often. This can be inefficient. It may be better to keep a big-enough version, and have a separate int tStevillength variable to keep track of how much is actually populated.

-
This code here:

for (int i = 0; i < tStevil.length - 1; i = i+2){
        if (i == 0){
            System.out.printf("(%.2f, %.2f)",tStevil[i],tStevil[i+1]);
        }
        else{
            System.out.printf(", (%.2f, %.2f)",tStevil[i],tStevil[i+1]);
        }
    }


would be better done with less code repeated:

for (int i = 0; i  0){
        System.out.print(", ");
    }
    System.out.printf("(%.2f, %.2f)",tStevil[i],tStevil[i+1]);
}


Algorithm.

Your algorithm is close to what I would have done. The only difference is that I would keep an array of indexes of the first member of a temperature pair....

Also, sometimes it is easier to loop from i = 1, instead of looping to i < length - 1

My code would look something like:

int pairlen = 0;
int[] pairs = new int[stevila.length];
double maxdiff = Double.MIN_VALUE;
for (int i = 1; i  maxdiff) {
        maxdiff = diff;
        pairlen = 0;
    }
    if (diff == maxdiff) {
        pairs[pairlen++] = [i - 1];
    }
}


This way, at the end of the loop, you have the index to the first pair of pairlen indexes for values with the largest diff.

You can then loop through that list using for (i = 0; i < pairlen; i++) { .... } and pull the actual values back from the stevila array.

This is not actually very different fdrom the way you have done it, but it is a bit neater.

Nice job.

Code Snippets

for (int i = 0; i < tStevil.length - 1; i = i+2){
        if (i == 0){
            System.out.printf("(%.2f, %.2f)",tStevil[i],tStevil[i+1]);
        }
        else{
            System.out.printf(", (%.2f, %.2f)",tStevil[i],tStevil[i+1]);
        }
    }
for (int i = 0; i < tStevil.length - 1; i = i+2){
    if (i > 0){
        System.out.print(", ");
    }
    System.out.printf("(%.2f, %.2f)",tStevil[i],tStevil[i+1]);
}
int pairlen = 0;
int[] pairs = new int[stevila.length];
double maxdiff = Double.MIN_VALUE;
for (int i = 1; i < stevila.length; i++) {
    double diff = Math.abs(stevila[i] - stevila[i - 1]);
    if (diff > maxdiff) {
        maxdiff = diff;
        pairlen = 0;
    }
    if (diff == maxdiff) {
        pairs[pairlen++] = [i - 1];
    }
}

Context

StackExchange Code Review Q#44691, answer score: 4

Revisions (0)

No revisions yet.