java - 使用 double 类型对对象数组列表的数组列表进行排序

标签 java sorting double multidimensional-array

我有一个数组列表,外部数组列表中的每个数组列表都包含以下值:holidayId、country、countryValue、duration、durationValue、accType、accTypeValue、holSubType、holSubTypeValue、weather、weatherValue、pricePP、pricePPValue、recommendationScore。这些都作为对象保存,我想知道是否有一种方法可以通过将其转换为 double 类型并以这种方式排序来使用recommendationScore对数组列表进行排序?或者有其他方法可以解决这个问题吗?

提前致谢

公共(public)类RecAlgorithmImpl {

public static ArrayList<Double> attRanking(ArrayList<Double> attRank){

    ArrayList<Double> attWeight = new ArrayList<>();
    double weight;

    /*Loops through the rankings given in the questionnaire 
     * and sets a new number for better recommendation accuracy */
    for(int i = 0; i < attRank.size();i++){

        if(attRank.get(i) == 1){
            attRank.set(i, 7.0);
        }
        else if(attRank.get(i) == 2){
            attRank.set(i, 6.0);
        }
        else if(attRank.get(i) == 3){
            attRank.set(i, 5.0);
        }
        else if(attRank.get(i) == 4){
            attRank.set(i, 4.0);
        }
        else if(attRank.get(i) == 5){
            attRank.set(i, 3.0);
        }
        else if(attRank.get(i) == 6){
            attRank.set(i, 2.0);
        }           
        else if(attRank.get(i) == 0){
            attRank.set(i, 1.0);
        }
    }

            //Loop through the ranked values and assign a weighting to each attribute
    for(int j = 0; j < attRank.size(); j++){
        weight = (attRank.get(j))/28;
        attWeight.add(weight);
    }

    for(int k = 0; k < attWeight.size();k++){
        System.out.println(attWeight.get(k));
    }
    return attWeight;
}

public static ArrayList<ArrayList> valuePoints(ArrayList<ArrayList> valuePoints){
        //DataRetrievalImpl database = new DataRetrievalImpl();
        ArrayList<ArrayList> holiday = new ArrayList<>();

        //test info
        ArrayList<String> test = new ArrayList<>();
        test.add("stuff1");
        test.add("stuff2");
        test.add("stuff3");
        test.add("stuff4");
        test.add("stuff5");
        test.add("stuff6");
        holiday.add(test);
        ArrayList<String> test1 = new ArrayList<>();
        test1.add("stuff11");
        test1.add("stuff12");
        test1.add("stuff13");
        test1.add("stuff14");
        test1.add("stuff15");
        test1.add("stuff16");
        holiday.add(test1);


        ArrayList<ArrayList> holidayScore = new ArrayList<>(); // creates new arraylist to hold the 6 attributes and their value points to be used in recommendation score
        //database information
        /*boolean condition = false;
        String[] select = null;
        String[] from = null;
        String[] where = null;
        holiday = database.getData(condition, select, from, where);*/

        //Loops through the holiday arraylist which contains all the holidays and adds the attributes that we need to the new one along with default points
        for(int i = 0; i < holiday.size(); i++){
            holidayScore.add(new ArrayList());
            holidayScore.get(i).add(holiday.get(i).get(0));
            holidayScore.get(i).add("0");
            holidayScore.get(i).add(holiday.get(i).get(1));
            holidayScore.get(i).add("0");
            holidayScore.get(i).add(holiday.get(i).get(2));
            holidayScore.get(i).add("0");
            holidayScore.get(i).add(holiday.get(i).get(3));
            holidayScore.get(i).add("0");
            holidayScore.get(i).add(holiday.get(i).get(4));
            holidayScore.get(i).add("0");
            holidayScore.get(i).add(holiday.get(i).get(5));
            holidayScore.get(i).add("0");
        }

        //Loops through the holidayScore arraylist checking the attributes against the valuePoints array list and 
        //modifying the value points where the two attribute values are equivalent in both arraylists
        //each if statement checks that each attrbute value is equivalent, successful matches record the points from valuePoints into holidayScore
        for(int j = 0; j < holidayScore.size(); j++){

            if(holidayScore.get(j).get(0) == valuePoints.get(j).get(0)){
                holidayScore.get(j).set(1, valuePoints.get(j).get(1));
            }

            if(holidayScore.get(j).get(2) == valuePoints.get(j).get(2)){
                holidayScore.get(j).set(3, valuePoints.get(j).get(3));
            }

            if(holidayScore.get(j).get(4) == valuePoints.get(j).get(4)){
                holidayScore.get(j).set(5, valuePoints.get(j).get(5));
            }

            if(holidayScore.get(j).get(6) == valuePoints.get(j).get(6)){
                holidayScore.get(j).set(7, valuePoints.get(j).get(7));
            }

            if(holidayScore.get(j).get(8) == valuePoints.get(j).get(8)){
                holidayScore.get(j).set(9, valuePoints.get(j).get(9));
            }

            if(holidayScore.get(j).get(10) == valuePoints.get(j).get(10)){
                holidayScore.get(j).set(11, valuePoints.get(j).get(11));
            }  
        }

        System.out.println(holiday);
        System.out.println("----------------------------------------------------");
        System.out.println(holidayScore);
        return holidayScore;
}

public static void recommendation(ArrayList<Double> weightedAttr, ArrayList<ArrayList> holidayScores){

        //each variable holds the current value points for that attribute value
        double countryValue;
        double durationValue;
        double accTypeValue;
        double holSubTypeValue;
        double weatherValue;
        double pricePPValue;
        double recScore;

        //Loops through the holidayScores arraylist convert the value points into double which is multiplied by the appropriate attribute weighting
        //this gives a decimal score for each attribute which is summed up for the final recommendation score and added to the end of the arraylist
        for(int k = 0; k < holidayScores.size(); k++){
            countryValue = Double.parseDouble(holidayScores.get(k).get(1).toString());
            durationValue = Double.parseDouble(holidayScores.get(k).get(3).toString());
            accTypeValue = Double.parseDouble(holidayScores.get(k).get(5).toString());
            holSubTypeValue = Double.parseDouble(holidayScores.get(k).get(7).toString());
            weatherValue = Double.parseDouble(holidayScores.get(k).get(9).toString());
            pricePPValue = Double.parseDouble(holidayScores.get(k).get(11).toString());
            recScore = (countryValue * weightedAttr.get(0));
            recScore = recScore + (durationValue * weightedAttr.get(1));
            recScore = recScore + (accTypeValue * weightedAttr.get(2));
            recScore = recScore + (holSubTypeValue * weightedAttr.get(3));
            recScore = recScore + (weatherValue * weightedAttr.get(4));
            recScore = recScore + (pricePPValue * weightedAttr.get(5));
            holidayScores.get(k).add(recScore);
        }

        System.out.println(holidayScores);
}

}

最佳答案

您应该实现一个对象,该对象保存数组列表(内部)中的所有值。然后,您可以轻松实现用于检查 recommendationScoreComparable

关于java - 使用 double 类型对对象数组列表的数组列表进行排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14993905/

相关文章:

java - 通过 ajax 发送 Map<String,List<String>> 作为请求参数

Java:以迭代方式向上舍入双值

java - 如何备份 Android 应用程序?

java - 我可以创建多个 STATIC 嵌套对象吗?

python - 在python中对类实例进行排序

javascript - 如何围绕最小值划分链表

PHP - 排序字母数字键的更有效方法

ios - 在 XCode 6 的 Swift 中将字符串加倍

objective-c - 如何获取字符串并从中删除部分以转换为 double

java - Hibernate 查询中出现错误。错误 : org. hibernate.hql.ast.QuerySyntaxException