Question

I'm trying to perform calculations on an array to sort it between greatest and least so that I can then compare it to the country it's associated with. I'm trying to do this by keeping one array variable in it's original format, while sorting a second temporary one, though when I attempt to sort the second it overwrites the original array. I'm quite a beginner, any help would be greatly appreciated!

I have my code here, it's kind of long, but I've written in "//" beside where the issue resides.

import java.util.*;
import java.io.*;

public class CarbonAnalysis{
    public static void main(String args[]) throws IOException{

        Scanner scan = new Scanner(System.in);
        String fileName;
        File file;
        Scanner in;

        do{
            try{
                System.out.println("Please enter a valid name of a file to be sorted");
                fileName = scan.next();
                file=new File(fileName);
            }catch(Exception IOException){
                System.out.println("Please enter a valid filename");
                fileName = scan.next();
                file=new File(fileName);
            }
        }while(file.exists()!=true);

        in = new Scanner(new File(fileName));

        in.nextLine();
        int rows = in.nextInt();
        System.out.println("There are " + rows + " Countries to compare.");

        GetSortPack(rows, in);

    }
    private static void GetSortPack(int rows, Scanner in){
        String[] country = new String[rows];
        double[] totalCO2 = new double[rows];
        double[] roadCO2 = new double[rows];
        double[] CO2PerPerson = new double[rows];
        double[] carsPerPerson = new double[rows];

        while(in.hasNext()){
            for(int j=0;j<rows;j++){
                for(int i=1;i<=5;i++){
                    if(i==1){
                        country[j] = in.next();
                    }
                    if(i==2){
                        totalCO2[j] = in.nextDouble();
                    }
                    if(i==3){
                        roadCO2[j] = in.nextDouble();
                    }
                    if(i==4){
                        CO2PerPerson[j] = in.nextDouble();
                    }
                    if(i==5){
                        carsPerPerson[j] = in.nextDouble();
                    }
                }
            }
        }//This is where the trouble begins.
        double[] temp = new double[rows];
        temp = totalCO2;

        System.out.println(totalCO2[0]);//At this stage, totalCO2 is still the correct value, however...

        double[] sortedTotalCO2=SelectionSort(temp);//Here it suddenly changes to equal what sortedTotalCO2 becomes...
        //when all that was supposed to occur in the called method (SelectionSort) were calculations and a returned value.

        System.out.println(sortedTCO2[0] + " " + totalCO2[0]);//Here, both values are equivalent.

        double[] sortedRoadCO2=SelectionSort(roadCO2);
        double[] sortedCO2PerPerson=SelectionSort(CO2PerPerson);
        double[] sortedCarsPerPerson=SelectionSort(carsPerPerson);

        for(int g=0;g<rows;g++){
            System.out.println(java.util.Arrays.binarySearch(sortedTotalCO2, totalCO2[g]);
        }
    }
    private static double[] SelectionSort(double[] a){
        int min=-1;

        for (int i = 0; i < a.length; i++) {
            min = i;
            for (int j = i + 1; j < a.length; j++) {
                if (a[min] > a[j]) {
                    min = j;
                }
            }
            if (min != i) {
                double temp = a[min];
                a[min] = a[i];
                a[i] = temp;
            }
    }
    return a;
    }
}
Was it helpful?

Solution

double[] temp = new double[rows]; // temp points to a new array
temp = totalCO2;                  // now you point to the array, totalCO2 points to
double[] sortedTotalCO2=SelectionSort(temp); // this function modifies the array temp, which is equal to the array totalCO2 points to

To fix this, change the SelectionSort(temp) method as following:

private static double[] SelectionSort(double[] b) {
    double[] a = new double[b.length];
    System.arraycopy(b, 0, a, 0, b.length);
    int min=-1;
    for (int i = 0; i < a.length; i++) {
        min = i;
        for (int j = i + 1; j < a.length; j++) {
            if (a[min] > a[j]) {
                min = j;
            }
        }
        if (min != i) {
            double temp = a[min];
            a[min] = a[i];
            a[i] = temp;
        }
    }
    return a;
}

Remove the lines:

double[] temp = new double[rows];
temp = totalCO2;

and change this line as following:

double[] sortedTotalCO2=SelectionSort(totalCO2);

OTHER TIPS

The problem is here:

double[] temp = new double[rows];
temp = totalCO2;

You're allocating a new array ans then you discard its reference to assign the other array's reference, so temp is now an alias of the first array. This line:

 temp = totalCO2;

doesn't copy the array, to copy it you have to use Arrays.copyOf

temp = Arrays.copyOf(totalCO2, rows);
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top