簡體   English   中英

使用雙精度類型對對象的數組列表的數組列表進行排序

[英]Sorting an arraylist of arraylists of objects using double type

我有一個數組列表的數組列表,外部數組列表中的每個數組列表都具有以下值holidayId,country,countryValue,duration,durationValue,accType,accTypeValue,holSubType,holSubTypeValue,天氣,weatherValue,pricePP,pricePPValue,recommendScore。 這些都是作為對象持有的,我想知道是否有一種方法可以通過使用compressionScore來對數組列表進行排序,方法是將其轉換為雙精度類型並以這種方式進行排序? 還是我可以解決此問題的任何其他方式?

提前致謝

公共類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);
}

}

您應該實現一個對象,該對象保存arraylist(內部)中的所有值。 然后你就可以輕松實現Comparable支票的recommendationScore

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM