首頁 > Java > java教程 > Java之真實專案中的章節排序實例

Java之真實專案中的章節排序實例

黄舟
發布: 2017-01-19 11:21:36
原創
1771 人瀏覽過

提出問題

真實項目中,章節如何排序? ? ?

解決問題

下面是真實專案中的一個章節排序問題,希望以後碰到該問題的程式設計師,可以提供一個參考,希望剛開始學程式設計的同學,可以當作一個練習的例子等等。圖片是思路,程式碼是實作。

Java之真實專案中的章節排序實例

1

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

69

70

71

72

73

74

75

76

77

78

79

80

81

82

83

84

85

86

87

88

89

90

91

92

93

94

95

96

97

98

99

100

101

102

103

104

105

106

107

108

109

110

111

112

113

114

115

116

117

118

119

120

121

122

123

124

125

126

127

128

129

130

131

132

133

134

135

136

137

138

139

140

141

142

143

144

145

146

147

148

149

150

151

152

153

154

155

156

157

158

159

160

161

162

163

164

165

166

167

168

169

170

171

172

173

174

175

176

177

[code]package com.hwy.test;

import java.util.*;

 

/**

 * 章节排序

 * Created by Ay on 2016/7/9.

 */

public class ChapterSortTest {

 

    public static void main(String[] args) {

 

        /** 从数据库加载数据 **/

        List<String> chapterList = getDataFromDB();

        /** 章节list转化为map **/

        Map<String,String> chapterMap =  listChangeToMap(chapterList);

        /** 获得章节号 **/

        List<String> chapterNum = getChapterNum(chapterMap);

        /** 章节号去除点 **/

        Map<Integer,String> chapterNumNoDot =  removeDot(chapterNum);

        /** 获取数字最大长度 **/

        int maxLength = getChapterNumMaxLength(chapterNumNoDot.keySet());

        /** 获取补0后的列表 **/

        List<String> fillZeroChapterNum = fillZero(maxLength,chapterNumNoDot);

        /** 排序 默认是升序,刚好是我们需要的**/

        Collections.sort(fillZeroChapterNum);

        /** 重组map对象**/

        List<String> sortChapterList =  getSortChapterMap(fillZeroChapterNum,chapterMap,chapterNumNoDot);

        /** 打印信息 **/

        if(sortChapterList != null){

            for(String key:sortChapterList){

                System.out.println(key);

            }

        }

 

    }

 

    /**

     * 获得排序后的map

     * @param fillZeroChapterNum

     * @param chapterMap

     * @return

     */

    public static List<String> getSortChapterMap(List<String> fillZeroChapterNum,Map<String,String> chapterMap,Map<Integer,String> chapterNotDot){

        if(null == fillZeroChapterNum || fillZeroChapterNum.size() == 0) return null;

        if(null == chapterMap) return null;

        List<String> sortChapterList = new ArrayList<>();

        for(String temp:fillZeroChapterNum){

            sortChapterList.add(chapterNotDot.get(Integer.parseInt(temp.replace("0", ""))) + " " + chapterMap.get(chapterNotDot.get(Integer.parseInt(temp.replace("0", "")))));

        }

        return sortChapterList;

    }

 

    /**

     * 补零操作

     * @return

     */

    public static List<String> fillZero(int maxLength,Map<Integer,String> chapterNumNoDot){

        if(null == chapterNumNoDot || chapterNumNoDot.size() ==0) return null;

        List<String> fillZeroList = new ArrayList<>();

        for(Integer key:chapterNumNoDot.keySet()){

            fillZeroList.add(key + getNeedZero(maxLength - (key + "").length()));

        }

        return fillZeroList;

    }

 

    /**

     * 获得需要0的个数

     * @param num

     * @return

     */

    public static String getNeedZero(int num){

        if(num <1) return "";

        StringBuffer sb = new StringBuffer();

        /** 拼凑需要的0 **/

        for(int i=0;i<num;i++){

            sb.append("0");

        }

        return sb.toString();

    }

 

    /**

     * 返回数组最大值

     * @param a

     * @return

     */

    public static int max(int[] a){

        // 返回数组最大值

        int x;

        int aa[]=new int[a.length];

        System.arraycopy(a,0,aa,0,a.length);

        x=aa[0];

        for(int i=1;i<aa.length;i++){

            if(aa[i]>x){

                x=aa[i];

            }

        }

        return x;

    }

 

    /**

     * 获得章节号最大长度

     * @param chapterNumNoDot

     * @return

     */

    public static int getChapterNumMaxLength(Set<Integer> chapterNumNoDot){

        if(null == chapterNumNoDot || chapterNumNoDot.size() == 0) return 0;

        Object[] chapterNumArr = chapterNumNoDot.toArray();

        int[] chapterNum = new int[chapterNumArr.length];

        for(int i=0;i<chapterNumArr.length;i++){

            chapterNum[i] = chapterNumArr[i].toString().length();

        }

        return max(chapterNum);

    }

 

    /**

     * 去除章节号中的点

     * @param chapterNumList

     * @return

     */

    public static Map<Integer,String> removeDot(List<String> chapterNumList){

        if(null == chapterNumList || chapterNumList.size() == 0) return null;

        Map<Integer,String> rmDotChapterNumMap = new HashMap<>();

        for(int i=0;i<chapterNumList.size();i++){

            /** 把点替换成空 **/

            rmDotChapterNumMap.put(Integer.parseInt(chapterNumList.get(i).replace(".","")),chapterNumList.get(i));

        }

        return rmDotChapterNumMap;

    }

 

    /**

     * 获取章节号

     * @param chapterMap

     * @return

     */

    public static List<String> getChapterNum(Map<String,String> chapterMap){

        if(null == chapterMap) return null;

        List<String> chapterNumList = new ArrayList<>();

        for(String chapterNum:chapterMap.keySet()){

            chapterNumList.add(chapterNum);

        }

        return chapterNumList;

    }

 

    /**

     * 把list转变为map

     * @param chapterList

     * @return

     */

    public static Map<String,String> listChangeToMap(List<String> chapterList){

        /** 存到map中 **/

        Map<String,String> chapterMap = new HashMap<>();

        if(null == chapterList || chapterList.size() == 0) return null;

        for(String chapter:chapterList){

            chapterMap.put(chapter.split(" ")[0], chapter.split(" ")[1]);

        }

        return chapterMap;

    }

 

    /**

     * 从数据库获取数据

     * @return

     */

    public static List<String> getDataFromDB(){

        List<String> chapterList = new ArrayList<>();

        chapterList.add("1.3.1 华丽新设计");

        chapterList.add("1.4 思想流派");

        chapterList.add("3.1 短小");

        chapterList.add("3.2 只做一件事");

        chapterList.add("2.11 别伴可爱");

        chapterList.add("4.4.12 注释掉的代码");

        chapterList.add("1.1 要有代码");

        chapterList.add("2.1 介绍");

        chapterList.add("8.5 使用尚不存在的代码");

        chapterList.add("5.3.1 水平方向上的区隔与靠近");

        return chapterList;

    }

}

登入後複製

以上就是Java之真實項目中的章節排序實例的內容,更多相關內容請關注PHP中文網(www.php.cn)!


本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板