Java中按字段对对象数组排序:“变量无法解析或不是字段”
我试图按字段对对象数组进行排序,但我一直收到这样的错误消息,即价格/位置无法解析或不是字段,即使它们都是类Realestate中声明的字段。在处理ArrayList时,我也遇到了同样的错误,即不是字段,所以我转向了简单的对象数组,我仍然会遇到同样的错误。我做错了什么?我找到了关于将数组与Comparable and Comparator进行比较的信息,但我不知道如何将任何结果应用于排序例程。请帮忙!这些是我的文件: 文件不动产:Java中按字段对对象数组排序:“变量无法解析或不是字段”,java,arrays,sorting,arraylist,Java,Arrays,Sorting,Arraylist,我试图按字段对对象数组进行排序,但我一直收到这样的错误消息,即价格/位置无法解析或不是字段,即使它们都是类Realestate中声明的字段。在处理ArrayList时,我也遇到了同样的错误,即不是字段,所以我转向了简单的对象数组,我仍然会遇到同样的错误。我做错了什么?我找到了关于将数组与Comparable and Comparator进行比较的信息,但我不知道如何将任何结果应用于排序例程。请帮忙!这些是我的文件: 文件不动产: public class Realestate { sta
public class Realestate {
static String location = "";
static String description = "";
static String price = "";
// constructor
public Realestate(String loc, String desc, String prc) {
location = loc;
description = desc;
price = prc;
}
}
文件RealestateFinder:
import java.util.*;
public class RealestateFinder {
// Hold properties' info
static String[] locations = {"Miami", "Las Vegas", "Paris", "London"};
static String[] descriptions = {"1-bedroom", "2-bedrooms", "3-bedrooms", "penthouse"};
static String[] prices = {"1,000,000", "2,000,000", "3,000,000", "4,000,000"};
// Sort per field
public static void sortArray(Realestate arr[]) {
for(int x = 0; x < arr.length; ++x) {
//use array Sort
Arrays.sort(arr);
}
}
public static void main(String[] args) {
int numProperties = 4;
//Create new Realestate object newProperty
Realestate[] newProperty = new Realestate[numProperties];
// Initialize array of objects Realestate[] newProperty using arrays from top
for(int x = 0; x < newProperty.length; ++x) {
newProperty[x] = new Realestate(locations[x], descriptions[x], prices[x]); // constructor
}
//Get user's input
//...
while(true) {
if(holdUserInput == 1) {
//sort per price
sortArray(newProperty.price);//price cannot be resolved or is not a field
break;
}
else if(holdUserInput == 2) {
// sort per location
sortArray(newProperty.location);//location cannot be resolved or is not a field
break;
}
else {
System.out.println("Program Ended \n");
break;
}// end else statement
}// end while loop
}// end main()
}// end class RealestateFinder
首先,您可能不会在3个字段中使用static,static意味着它与特定实例无关,这不是您想要的 然后使用列表更容易排序,因此:
int numProperties = 4;
List<Realestate> list = new ArrayList<>();
for (int x = 0; x < numProperties; ++x) {
list.add(new Realestate(locations[x], descriptions[x], prices[x]));
}
最后,由于在发生故障和中断时使用没有意义,因此最好使用更好的开关:
switch (holdUserInput) {
case 1:
list.sort(Comparator.comparing(Realestate::getPrice));
break;
case 2:
list.sort(Comparator.comparing(Realestate::getLocation));
break;
case 3:
list.sort(Comparator.comparing(Realestate::getDescription));
break;
default:
System.out.println("Program Ended \n");
break;
}
是的,newProperty是一个数组-数组没有价格。。。听起来你可能想看看你的问题有多个与你的问题无关的问题谢谢,@azro,你的回答真的很有帮助,不仅仅是因为这段代码,还有我对一些概念的理解:
switch (holdUserInput) {
case 1:
list.sort(Comparator.comparing(Realestate::getPrice));
break;
case 2:
list.sort(Comparator.comparing(Realestate::getLocation));
break;
case 3:
list.sort(Comparator.comparing(Realestate::getDescription));
break;
default:
System.out.println("Program Ended \n");
break;
}