我想采用这种格式:
//Game, Team, Player, Position, Order, Sub----Not part of file
331027,24,7912,CF,1,1
331028,22,7913,P,1,1
331028,22,5909,1B,2,1
331028,22,8394,P,2,2
根据给定顺序中sub的更高数字显示此数据(顺序),输出将变为:
331027,24,7912,CF,1 //player 7912 goes first for team 24
331028,22,7913,P,1 //player 7913 goes first for team 22
331028,22,8394,P,2 // player 8394 goes second for team 22 because he/she has higher 'Sub' order
更新:
import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class BattingOrder {
String game_ID;
String team_ID;
String player_ID;
String position;
String battingOrder;
String subOrder;
public BattingOrder(String game, String team, String player, String place,
String batter, String sub) {
game_ID = game;
team_ID = team;
player_ID = player;
position = place;
battingOrder = batter;
subOrder = sub;
}
@Override
public String toString() {
return game_ID + "," + team_ID + "," + player_ID + "," + position + ","
+ battingOrder;
}
public static void main(String[] args) throws IOException {
FileInputStream fstream = new FileInputStream(
"BatterInfo.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
List<BattingOrder> sortList = new ArrayList<BattingOrder>();
for (String line; (line = br.readLine()) != null;) {
String delims = "[,]";
String[] parsedData = line.split(delims);
sortList.add(new BattingOrder(parsedData[0], parsedData[1],
parsedData[2], parsedData[3], parsedData[4], parsedData[5]));
}
for (BattingOrder order : sortList) {
System.out.println(order);
}
br.close();
}
}
当前输出:
331027,24,7912,CF,1
331028,22,7913,P,1
331028,22,5909,1B,2 //This should be replaced by bottom 'string' because the subOrder is higher.
331028,22,8394,P,2
我想:
331027,24,7912,CF,1
331028,22,7913,P,1
331028,22,8394,P,2
伪代码中的逻辑怎么看?
答案 0 :(得分:1)
来自http://www.mkyong.com/java/how-to-sort-an-arraylist-in-java/和http://www.mkyong.com/java/java-object-sorting-example-comparable-and-comparator/以及来自Java Devil的上述答案
我认为最好的方法是将每个BattingOrder对象放入一个集合,即ArrayList并使用Collections.sort
List<BattingOrder> unsortList = new ArrayList<BattingOrder>();
向BattingOrder添加构造函数,或者只需在main中的for循环中手动设置值,以从文件中分割输入字符串或接受预先准备的值 然后在for循环完成后调用Collections.sort并传入一个自定义Comparator,你已经注释掉大部分所需的代码应该没有问题。
for (String line; (line = br.readLine()) != null;) {
// String delims = "[,]";
// String[] parsedData = line.split(delims);
//Split and assign the values to a new BattingOrder object here either in a
//constructor for BattingObject or here and pass in the values from the file
unsortList.add(new BattingOrder(line));
}
循环完成后,调用我们的自定义排序函数
Collections.sort(unsortList ,new Comparator<BattingOrder>(){
@Override
public int compare(BattingOrder one, BattingOrder two) {
Integer orderOne = Integer.parseInt(one.battingOrder);
Integer orderTwo = Integer.parseInt(two.battingOrder);
//ascending order
return orderOne.compareTo(orderTwo);
//descending order
//return orderTwo.compareTo(orderOne);
}
});
和bam应该排序
我无法在没有Java编译器的情况下测试此代码atm,但如果不正确则应该关闭
祝你好运;)我希望你可以在一夜之间添加一些细节(带索引的原始输入)
Index Pos Order Sub 1 CF 1 1 2 P 1 1 3 1B 2 1 4 P 2 2
你没有足够的排序逻辑,可以用较低的subOrder消失索引3但是1&amp; 2是可以互换的,因为它们都有1的subOrder和1的顺序,所以首先读到的将是我认为的列表的顶部。
此代码应按随机顺序对它们进行排序(由它们基本上读入的顺序决定) 然后将删除任何具有匹配的battingOrder和较低的subOrder
我希望那些接近你想要的东西无论如何排序是非常好的只是不是很好的逻辑删除不是很有效但是我必须继续atm
通过将整数存储为整数而不是字符串
,可以删除所有强制转换 import java.io.BufferedReader;
import java.io.DataInputStream;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public class BattingOrder
{
String game_ID;
String team_ID;
String player_ID;
String position;
String battingOrder;
String subOrder;
public BattingOrder(String game, String team, String player, String place, String batter, String sub) {
game_ID = game;
team_ID = team;
player_ID = player;
position = place;
battingOrder = batter;
subOrder = sub;
}
@Override
public String toString()
{
return game_ID + "," + team_ID + "," + player_ID + "," + position + "," + battingOrder;
}
public static void main(String[] args) throws IOException
{
FileInputStream fstream = new FileInputStream("BatterInfo.txt");
DataInputStream in = new DataInputStream(fstream);
BufferedReader br = new BufferedReader(new InputStreamReader(in));
String delims = "[,]";
List<BattingOrder> battingOrders = new ArrayList<BattingOrder>();
for (String line; (line = br.readLine()) != null;)
{
String[] parsedData = line.split(delims);
battingOrders.add(new BattingOrder(parsedData[0], parsedData[1], parsedData[2], parsedData[3], parsedData[4], parsedData[5]));
}
br.close();
System.out.println("Unordered");
for (BattingOrder order : battingOrders)
{
System.out.println(order);
}
Collections.sort(battingOrders ,new Comparator<BattingOrder>(){
@Override
public int compare(BattingOrder one, BattingOrder two)
{
if(one.battingOrder.equals(two.battingOrder))
{
Integer subOrderOne = Integer.parseInt(one.subOrder);
Integer subOrderTwo = Integer.parseInt(two.subOrder);
return subOrderOne.compareTo(subOrderTwo);
}
Integer orderOne = Integer.parseInt(one.battingOrder);
Integer orderTwo = Integer.parseInt(two.battingOrder);
return orderOne.compareTo(orderTwo);
}
});
System.out.println("Ordered");
for (BattingOrder order : battingOrders)
{
System.out.println(order);
}
List<BattingOrder> toDelete = new ArrayList<BattingOrder>();
for (BattingOrder one : battingOrders)
{
for (BattingOrder two : battingOrders)
{
if(one.battingOrder.equals(two.battingOrder))
{
Integer subOrderOne = Integer.parseInt(one.subOrder);
Integer subOrderTwo = Integer.parseInt(two.subOrder);
if(subOrderOne < subOrderTwo)
{
toDelete.add(one);
}
else if(subOrderOne > subOrderTwo)
{
toDelete.add(two);
}
}
}
}
battingOrders.removeAll(toDelete);
System.out.println("Final");
for (BattingOrder order : battingOrders)
{
System.out.println(order);
}
}
}
答案 1 :(得分:0)
您需要先读取文本文件中的所有行,然后对它们进行排序。
当你读取每一行时,将它存储到一个字符串或更好的列表中,然后创建一个代表每一行的对象 - 你已经在这里拥有了它。因此,对于每行,您可能希望在您有注释时用逗号分隔并将值分配给对象的新实例,然后将其添加到列表中。
然后,您可以使用比较器对列表中的这些对象进行排序,使用类似这样的
Collections.sort(yourList,new Comparator<BattingOrder>(){
@Override
public int compare(BattingOrder b1, BattingOrder b2)
{
//Your comparison code goes here
}
});