我正在尝试对数组执行计算,以将其在最大和最小之间进行排序,以便我可以将其与与其关联的国家/地区进行比较。我试图通过将一个数组变量保留为原始格式来实现此目的,同时对第二个临时变量进行排序,尽管当我尝试对第二个数组变量进行排序时,它会覆盖原始数组。我是一个初学者,任何帮助将不胜感激!
我的代码在这里,有点长,但我在问题所在的位置旁边写了“//”。
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;
}
}
最佳答案
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);
关于java - 仅对一个数组执行计算时,如何防止两个数组相等?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22860833/