我在记事本中创建了一个书库,如下所示:
Author | Name of the book | Availability? | Readers Code | Return Date
----------------------------------------------------------------------------
J. K. Rowling Harry Potter Yes - -
Mark Sullivan The Black Book Yes - -
Margaret Atwood Rogue Yes - -
我需要按照字母顺序对这些书的名称进行排序。 使用\ t分隔单词。 我怎么能这样做?
This is the code that adds new books :
try (BufferedWriter bw = new BufferedWriter(
new FileWriter("C:/Users/Name/Desktop/library.txt", true))) {
System.out.print("Enter authors name\t");
name = sc.nextLine();
name = sc.nextLine();
author[l] = name;
System.out.print("Enter books name\t");
name = sc.nextLine();
bookname[l] = name;
vaiBib[l] = "Yes";
BilNr[l] = "-";
AtgDat[l] = "-";
l++;
if ((author[l- 1] != null) && (bookname[l- 1] != null)) {
content = bookname[l- 1] + "\t\t" + author[l- 1] + "\t\t" + vaiBib[l- 1]
+ "\t\t\t" + BilNr[l- 1] + "\t\t" + AtgDat[l- 1];
bw.write(content);
bw.newLine();
}
} catch (IOException e) {
e.printStackTrace();
}
使用此代码,我可以使用记事本中的行创建链接列表:
BufferedReader br = new BufferedReader(new FileReader("C:/Users/Name/Desktop/library.txt"));
ArrayList<String> Line2= new ArrayList<>();
while ((line = br.readLine()) != null) {
Line2.add(line);
}
答案 0 :(得分:0)
如果您可以使用Java 8:
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<select id="sel1" class="remRep">
<option value="0">none</option>
<option value="1">Subject1</option>
<option value="2">Subject2</option>
<option value="3">Subject3</option>
</select>
<select id="sel2" class="remRep">
<option value="0">none</option>
<option value="1">Subject1</option>
<option value="2">Subject2</option>
<option value="3">Subject3</option>
<option value="4">Subject4</option>
<option value="5">Subject5</option>
</select>
<select id="sel3" class="remRep">
<option value="0">none</option>
<option value="1">Subject1</option>
<option value="2">Subject2</option>
<option value="3">Subject3</option>
<option value="4">Subject4</option>
<option value="5">Subject5</option>
</select>
在早期的Java版本中,您需要编写一个Line2.sort(Comparator.comparing(s -> s.substring(s.indexOf('\t') + 1)));
类,您可以在Internet上找到大量的示例和教程。通过获取Comparator
来比较第一个选项卡之后的字符串部分的基本思路仍然有效,您只需要为要比较的两个字符串中的每个字符串执行此操作。