gpt4 book ai didi

java - Java中可以使用哪个实用程序类来存储和应用排序

转载 作者:行者123 更新时间:2023-12-01 23:31:45 26 4
gpt4 key购买 nike

我有点困惑哪些实用程序类可以用于解决此类问题:
我有一个文件 Movies.txt,其中包含以下信息:IdNameDirectorRating。评级可能存在也可能不存在。

示例:

1,ABC,Mr. xyz,4.5
3,GHI,Mr. mno
2,DEF,Ms. stu,3

我需要读取此文件并将其存储到内存中,然后应用按评级名称排序,然后稍后写入该文件。在这种情况下,哪个实用程序类最能帮助我,这样如果可能的话,可以轻松地做到这一点。没有更多文件可供使用。

最佳答案

首先定义 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();
}

}

}

关于java - Java中可以使用哪个实用程序类来存储和应用排序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19172800/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com