我有点困惑,哪种实用程序类可用于此类问题:
我有一个包含以下信息的Movies.txt文件:Id
,Name
,Director
,Rating
。评级可能存在也可能不存在。
样品:
1,ABC,Mr. xyz,4.5
3,GHI,Mr. mno
2,DEF,Ms. stu,3
我需要读取该文件并将其存储到内存中,然后按rating
和name
进行排序,然后再写入该文件。
在这种情况下,哪个实用程序类可以最好地帮助我,因此如果可能的话,可以轻松地执行此操作。没有更多文件可供使用。
答案 0 :(得分:1)
您需要一次读取此输入文件一行,通过拆分'解析每一行,构建一个Movie对象(您定义)并添加到某种数组/映射/ set。然后根据指令对数组/ map / set进行排序,并写出响应文件。
做一些研究:
答案 1 :(得分:1)
首先定义描述“电影”基本属性的Object
。让你的生活更轻松,直接implement Comparable<Movie>
可能是个好主意。
public class Movie implements Comparable<Movie> {
private int id;
private String name;
private String directory;
private double rating;
public Movie(int id, String name, String directory, double rating) {
this.id = id;
this.name = name;
this.directory = directory;
this.rating = rating;
}
public int getId() {
return id;
}
public String getDirectory() {
return directory;
}
public String getName() {
return name;
}
public double getRating() {
return rating;
}
@Override
public int compareTo(Movie o) {
int diff = (int) asInt(getRating()) - asInt(o.getRating());
if (diff == 0) {
diff = getName().compareTo(name);
}
return diff;
}
protected int asInt(double value) {
String text = Double.toString(value);
text = text.replaceAll("\\.", "");
return Integer.parseInt(text);
}
@Override
public String toString() {
return getId() + ", " + getName() + ", " + getDirectory() + ", " + getRating();
}
}
创建List
以保留传入的电影
List<Movie> movies = new ArrayList<Movie>(25);
阅读内容并将每一行解析为各自的属性元素(我会留给你),将每个新创建的Movie
添加到列表中......
movies.add(new Movie(...));
使用Collections.sort(movies)
对它们进行排序......
例如......
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
public class SortExample {
public static void main(String[] args) {
List<Movie> movies = new ArrayList<Movie>(5);
movies.add(new Movie(1, "ABC", "Mr. xyz", 4.5));
movies.add(new Movie(2, "GHI", "Mr. mno", 0));
movies.add(new Movie(3, "DEF", "Ms. stu", 3));
movies.add(new Movie(4, "AT1", "Mr. T", 3));
System.out.println("Before....");
for (Movie movie : movies) {
System.out.println(movie);
}
Collections.sort(movies);
System.out.println("After....");
for (Movie movie : movies) {
System.out.println(movie);
}
}
public static class Movie implements Comparable<Movie> {
private int id;
private String name;
private String directory;
private double rating;
public Movie(int id, String name, String directory, double rating) {
this.id = id;
this.name = name;
this.directory = directory;
this.rating = rating;
}
public int getId() {
return id;
}
public String getDirectory() {
return directory;
}
public String getName() {
return name;
}
public double getRating() {
return rating;
}
@Override
public int compareTo(Movie o) {
int diff = (int) asInt(getRating()) - asInt(o.getRating());
if (diff == 0) {
diff = getName().compareTo(name);
}
return diff;
}
protected int asInt(double value) {
String text = Double.toString(value);
text = text.replaceAll("\\.", "");
return Integer.parseInt(text);
}
@Override
public String toString() {
return getId() + ", " + getName() + ", " + getDirectory() + ", " + getRating();
}
}
}
答案 2 :(得分:1)
好的,答案已被接受,但我有权不同意。
可比较应该反映基于其整个状态的2个对象之间的关系(当然,跳过id和其他不相关的字段)。如果您想按部分状态(几个字段)订购某些对象,则应使用Comparator。