在Java中按字段排序对象数组:“变量无法解析或不是字段”

时间:2022-12-13 20:53:29

I am trying to sort an array of objects by field, but I keep getting this error saying that "price / location cannot be resolved or is not a field", even though they both are fields, declared in the class Realestate. I got the same error about "not being a field" when I was dealing with ArrayList as well, so I moved to simple arrays of objects and I'm still getting the same error. What am I doing wrong? I found info about comparing arrays with Comparable and Comparator but I don't undertand how to apply whatever results to the sorting routine. Please HELP! These are my files:

我试图按字段对对象数组进行排序,但我不断收到此错误,说“价格/位置无法解析或不是字段”,即使它们都是字段,在类Realestate中声明。当我处理ArrayList时,我得到了关于“不是一个字段”的相同错误,所以我转移到简单的对象数组,我仍然得到相同的错误。我究竟做错了什么?我找到了有关将数组与Comparable和Comparator进行比较的信息,但我没有说明如何将任何结果应用于排序例程。请帮忙!这些是我的文件:

File Realestate:

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

File 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

1 个解决方案

#1


2  

First you may not use static for your 3 fields, static means that it's not relative to a particular instance, which i not what you want to

首先,你可能不会为你的3个字段使用静态,静态意味着它不是与特定实例相关,我不是你想要的

Then use a List is easier to sort so :

然后使用List更容易排序,所以:

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

Then, use existing methods to sort is quicker and easier to use :

然后,使用现有方法进行排序更快更容易使用:

while (true) {
    if (holdUserInput == 1) {
        list.sort(Comparator.comparing(Realestate::getPrice));
        break;
    } else if (holdUserInput == 2) {
        list.sort(Comparator.comparing(Realestate::getLocation));
        break;
    } else if (holdUserInput == 2) {
        list.sort(Comparator.comparing(Realestate::getDescription));
        break;
    } else {
        System.out.println("Program Ended \n");
        break;
    }// end else statement
}

And finally, because there is not sense to use while(true) and break, better use switch which is better :

最后,因为没有意义使用while(true)和break,所以最好使用更好的switch:

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

#1


2  

First you may not use static for your 3 fields, static means that it's not relative to a particular instance, which i not what you want to

首先,你可能不会为你的3个字段使用静态,静态意味着它不是与特定实例相关,我不是你想要的

Then use a List is easier to sort so :

然后使用List更容易排序,所以:

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

Then, use existing methods to sort is quicker and easier to use :

然后,使用现有方法进行排序更快更容易使用:

while (true) {
    if (holdUserInput == 1) {
        list.sort(Comparator.comparing(Realestate::getPrice));
        break;
    } else if (holdUserInput == 2) {
        list.sort(Comparator.comparing(Realestate::getLocation));
        break;
    } else if (holdUserInput == 2) {
        list.sort(Comparator.comparing(Realestate::getDescription));
        break;
    } else {
        System.out.println("Program Ended \n");
        break;
    }// end else statement
}

And finally, because there is not sense to use while(true) and break, better use switch which is better :

最后,因为没有意义使用while(true)和break,所以最好使用更好的switch:

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