Java 在仅对一个阵列执行计算时,如何防止两个阵列变得相等?
我试图对一个数组进行计算,在最大值和最小值之间进行排序,这样我就可以将它与它关联的国家进行比较。我试图通过保持一个数组变量的原始格式来实现这一点,同时对第二个临时变量进行排序,尽管当我尝试对第二个变量进行排序时,它会覆盖原始数组。我是一个初学者,任何帮助都将不胜感激 我这里有我的代码,有点长,但我在问题所在的旁边写了“/”Java 在仅对一个阵列执行计算时,如何防止两个阵列变得相等?,java,arrays,sorting,operations,Java,Arrays,Sorting,Operations,我试图对一个数组进行计算,在最大值和最小值之间进行排序,这样我就可以将它与它关联的国家进行比较。我试图通过保持一个数组变量的原始格式来实现这一点,同时对第二个临时变量进行排序,尽管当我尝试对第二个变量进行排序时,它会覆盖原始数组。我是一个初学者,任何帮助都将不胜感激 我这里有我的代码,有点长,但我在问题所在的旁边写了“/” import java.util.*; import java.io.*; public class CarbonAnalysis{ public static v
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;
}
}
import java.util.*;
导入java.io.*;
公共级碳分析{
公共静态void main(字符串args[])引发IOException{
扫描仪扫描=新扫描仪(System.in);
字符串文件名;
文件;
扫描仪输入;
做{
试一试{
System.out.println(“请输入要排序的文件的有效名称”);
fileName=scan.next();
文件=新文件(文件名);
}捕获(异常IOException){
System.out.println(“请输入有效的文件名”);
fileName=scan.next();
文件=新文件(文件名);
}
}while(file.exists()!=true);
in=新扫描仪(新文件(文件名));
in.nextLine();
int rows=in.nextInt();
System.out.println(“有“+行+国家进行比较。”);
GetSortPack(行,英寸);
}
私有静态void GetSortPack(int行,扫描仪输入){
字符串[]国家=新字符串[行];
double[]totalCO2=新的双[行];
double[]roadCO2=新的双[行];
double[]CO2PerPerson=新的double[行];
double[]carsPerPerson=新的double[行];
while(在.hasNext()中){
对于(int j=0;j
要解决此问题,请更改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[] sortedTotalCO2=SelectionSort(totalCO2);
并将此行更改如下:
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[] sortedTotalCO2=SelectionSort(totalCO2);
要解决此问题,请更改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[] sortedTotalCO2=SelectionSort(totalCO2);
并将此行更改如下:
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[] sortedTotalCO2=SelectionSort(totalCO2);
要解决此问题,请更改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[] sortedTotalCO2=SelectionSort(totalCO2);
并将此行更改如下:
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[] sortedTotalCO2=SelectionSort(totalCO2);
要解决此问题,请更改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[] sortedTotalCO2=SelectionSort(totalCO2);
并将此行更改如下:
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[] sortedTotalCO2=SelectionSort(totalCO2);
问题在于:
double[] temp = new double[rows];
temp = totalCO2;
您正在分配一个新数组,然后放弃其引用以分配另一个数组的引用,因此temp现在是第一个数组的别名。
这一行:
temp = totalCO2;
不复制数组,要复制它,必须使用Arrays.copyOf
temp = Arrays.copyOf(totalCO2, rows);
问题在于:
double[] temp = new double[rows];
temp = totalCO2;
您正在分配一个新数组,然后放弃其引用以分配另一个数组的引用,因此temp现在是第一个数组的别名。
这一行:
temp = totalCO2;
不复制数组,要复制它,必须使用Arrays.copyOf
temp = Arrays.copyOf(totalCO2, rows);
问题在于:
double[] temp = new double[rows];
temp = totalCO2;
您正在分配一个新数组,然后放弃其引用以分配另一个数组的引用,因此temp现在是第一个数组的别名。
这一行:
temp = totalCO2;
不复制数组,要复制它,必须使用Arrays.copyOf
temp = Arrays.copyOf(totalCO2, rows);
问题在于:
double[] temp = new double[rows];
temp = totalCO2;
您正在分配一个新数组,然后放弃其引用以分配另一个数组的引用,因此temp现在是第一个数组的别名。
这一行:
temp = totalCO2;
不复制数组,要复制它,必须使用Arrays.copyOf
temp = Arrays.copyOf(totalCO2, rows);
正如@Harmlez在其第一个代码示例中所显示的,您使用的是指针而不是引用。要了解有关指针和引用的更多信息,您可以像@Harmlez在其第一个代码示例中所显示的那样,使用指针而不是引用进行签出。要了解有关指针和引用的更多信息,您可以像@Harmlez显示的那样进行签出在他的第一个代码示例中,您使用的是指针而不是引用。要了解有关指针和引用的更多信息,您可以像@Harmlez在他的第一个代码示例中所显示的那样,使用指针而不是引用进行查看。要了解有关指针和引用的更多信息,您可以查看Awesome,只需添加一个额外的结尾}确保“a”是双精度[]而不是简单的双精度,但这非常有效。非常感谢!太棒了,只需添加一个额外的结尾},并确保“a”是双精度[]而不是简单的双精度,但这非常有效。非常感谢!太棒了,只需添加一个额外的结尾},并确保“a”是双精度[]而不是简单的双精度[],但这非常有效。非常感谢!太棒了,只需添加一个额外的结尾},并确保“a”是双精度[]而不是简单的双精度[],但这非常有效。非常感谢!