在仅对一个执行计算时,如何防止两个数组变得相等?

时间:2014-04-04 10:56:06

标签: java arrays sorting operations

我尝试对数组执行计算以在最大和最小之间对其进行排序,以便我可以将其与其关联的国家进行比较。我尝试通过将一个数组变量保留在原始格式中,同时排序第二个临时格式来尝试这样做,但是当我尝试排序第二个时,它会覆盖原始数组。我是一个非常初学者,非常感谢任何帮助!

我的代码在这里,它有点长,但我写的是" //"在问题所在的地方旁边。

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;
    }
}

2 个答案:

答案 0 :(得分:3)

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

要解决此问题,请按以下步骤更改SelectionSort(temp)方法:

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;
}

删除行:

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

并按以下方式更改此行:

double[] sortedTotalCO2=SelectionSort(totalCO2);

答案 1 :(得分:0)

问题在于:

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

你正在分配一个新的数组,然后你放弃它的引用来分配另一个数组的引用,所以temp现在是第一个数组的别名。 这一行:

 temp = totalCO2;

不复制数组,复制它你必须使用Arrays.copyOf

temp = Arrays.copyOf(totalCO2, rows);