# Working with list in Java

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 < stevila.length - 1; i++){
double a = stevila[i];
double b = stevila[i+1];
double absRaz = a - b;
if (absRaz < 0){
absRaz = -1 * absRaz;
}
if (absRaz == maxRaz){
double tStevilStara[] = tStevil;
tStevil = new double[tStevilStara.length+2];
tStevil[tStevil.length - 2] = a;
tStevil[tStevil.length - 1] = b;
for (int t = 0; t < tStevilStara.length; t++){
tStevil[t] = tStevilStara[t];
}
}
else if (absRaz > 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);
}
}

• @Vogel612: I got it now. It just needed CTRL+K. Commented Mar 18, 2014 at 19:24
• @Jamal I hate to put such stuff throught edit-queue and not even notify OP about it... It feels so intrusive.. Commented Mar 18, 2014 at 19:26
• @Vogel612: That's just the nature of it. In most cases, others will review the edits. Commented Mar 18, 2014 at 19:27
• @user2956514 The most obvious advice would be just use an ArrayList save yourself a lot of trouble, but I'm guessing this is a homework and you are supposed to use arrays. Your naming is probably bad but you can't get them reviewed because you didn't care to translate them. Commented Mar 19, 2014 at 8:17

## 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 < tStevil.length - 1; i = i+2){
if (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 < 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];
}
}


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.

• I really appreciate you taking time and for giving me a great answer :) Commented Mar 19, 2014 at 9:11