Java 是否通过对象字段筛选/排序集合?
我不知道这为什么不起作用。我不确定这是打印的问题,还是方法本身的问题。 我正在制作一个程序,根据给定的用户输入对歌曲集进行过滤或排序。用户应该能够输入多个命令以进一步缩小列表范围 我的FilterLink和filterYear方法工作得非常好,但其他方法最终打印出一组看似随机的歌曲,无论输入的标题或艺术家是什么,这些歌曲都不会改变,通常只会在经过非常长的等待时间和一长串空格后才会出现 即使在打印了这组混合的歌曲之后,程序也不会终止,并定期在控制台中输出一个空格,就像在System.out.println()语句中连续运行一样 如果删除配置输出文件的代码(这是项目的一项要求),则这些方法将无法完全打印。无论这些更改如何,FilterLink和filterYear都能继续完美地工作 我的排序方法也会出现这个问题。无论我运行什么排序方法,它仍然会打印出空格和随机歌曲,或者根本不打印 有什么我遗漏的吗?我已经尝试打印出变量,并在程序中战略性地插入System.out.println(“test”),以确定程序是什么,但它似乎正确地解析了输入,并且这些方法确实正在成功运行。 否则我就无法找出问题所在 我能得到帮助来确定我遗漏了什么吗?尽管我花了两个小时仔细研究我的代码,我还是搞不清楚我的逻辑错误是什么 以下是相关代码: 主要类别:Java 是否通过对象字段筛选/排序集合?,java,sorting,collections,arraylist,filter,Java,Sorting,Collections,Arraylist,Filter,我不知道这为什么不起作用。我不确定这是打印的问题,还是方法本身的问题。 我正在制作一个程序,根据给定的用户输入对歌曲集进行过滤或排序。用户应该能够输入多个命令以进一步缩小列表范围 我的FilterLink和filterYear方法工作得非常好,但其他方法最终打印出一组看似随机的歌曲,无论输入的标题或艺术家是什么,这些歌曲都不会改变,通常只会在经过非常长的等待时间和一长串空格后才会出现 即使在打印了这组混合的歌曲之后,程序也不会终止,并定期在控制台中输出一个空格,就像在System.out.pri
public static void main(String[] args) throws FileNotFoundException, IOException{
//user greeting statements and instructions
//scanning file, ArrayList declaration
Scanner input = new Scanner(System.in);
while (input.hasNextLine()) {
int n = 0;
SongCollection collection = new SongCollection(songs);
String inputType = input.nextLine();
String delims = "[ ]";
String[] tokens = inputType.split(delims);
for (int i = 0; i < tokens.length; i++) {
n = 0;
if (n == 0) {
if ((tokens[i]).contains("year:")) {
collection.filterYear(Range.parse(tokens[i]));
n = 1;
}// end of year loop
if ((tokens[i]).contains("rank:")) {
collection.filterRank(Range.parse(tokens[i]));
n = 1;
}// end of rank
if ((tokens[i]).contains("artist:")) {
collection.filterArtist(tokens[i]);
n = 1;
}// end of artist
if ((tokens[i]).contains("title:")) {
collection.filterTitle(tokens[i]);
n = 1;
}// end of title
if ((tokens[i]).contains("sort:")) {
if ((tokens[i]).contains("title")) {
collection.sortTitle();
n = 1;
}// end of sort title
if ((tokens[i]).contains("artist")) {
collection.sortArtist();
n = 1;
}// end of sort artist
if ((tokens[i]).contains("rank")) {
collection.sortRank();
n = 1;
}// end of sort rank
if ((tokens[i]).contains("year")) {
collection.sortYear();
n = 1;
}// end of sort year
}//end of sort
}// end of for loop
}// end of input.hasNextline loop
/*final PrintStream console = System.out; //saves original System.out
File outputFile = new File("output.txt"); //output file
PrintStream out = new PrintStream(new FileOutputStream(outputFile)); //new FileOutputStream
System.setOut(out); //changes where data will be printed
*/ System.out.println(collection.toString());
/*System.setOut(console); //changes output to print back to console
Scanner outputFileScanner = new Scanner(outputFile); //inputs data from file
while ((outputFileScanner.hasNextLine())) { //while the file still has data
System.out.println(outputFileScanner.nextLine()); //print
}
outputFileScanner.close();
out.close();*/
}
}// end of main
}// end of class
publicstaticvoidmain(字符串[]args)抛出FileNotFoundException、IOException{
//用户问候语和说明
//扫描文件,ArrayList声明
扫描仪输入=新扫描仪(System.in);
while(input.hasNextLine()){
int n=0;
歌曲集=新歌曲集(歌曲);
字符串inputType=input.nextLine();
字符串delims=“[]”;
String[]tokens=inputType.split(delims);
for(int i=0;i
SongCollection类及其各自的筛选和排序方法:
import java.io.File;
import java.io.FileNotFoundException;
import java.util.*;
public class SongCollection {
ArrayList<Song> songs2;
ArrayList<Song> itemsToRemove = new ArrayList<Song>(); // second collection
// for items to
// remove
public SongCollection(ArrayList<Song> songs) { // constructor for SongCollection
System.out.println("Test");
this.songs2 = songs;
}
public void filterYear(Range r) {
int n = 0;
if (n == 0) {
System.out.println("Program is processing.");
n++;
for (Song song1 : songs2) {
if (song1.year > (r.getMax()) || (song1.year) < (r.getMin())) {
itemsToRemove.add(song1);
}
}
songs2.removeAll(itemsToRemove);
itemsToRemove.clear();
}
}
public void filterRank(Range r) {
int n = 0;
if (n == 0) {
System.out.println("Program is processing.");
n++;
for (Song song1 : songs2) {
if (song1.rank > (r.getMax()) || (song1.rank) < (r.getMin())) {
itemsToRemove.add(song1);
}
}
songs2.removeAll(itemsToRemove);
itemsToRemove.clear();
}
}
public void filterArtist(String s) {
int n = 0;
if (n == 0) {
System.out.println("Program is processing.");
n++;
for (Song song1 : songs2) {
if ((!(((song1.artist).contains(s))))) {
itemsToRemove.add(song1);
}
}
songs2.removeAll(itemsToRemove);
itemsToRemove.clear();
}
}
public void filterTitle(String s) {
int n = 0;
if (n == 0) {
System.out.println("Program is processing.");
n++;
for (Song song1 : songs2) {
if ((!(((song1.title).contains(s))))) {
itemsToRemove.add(song1);
}
}
songs2.removeAll(itemsToRemove);
itemsToRemove.clear();
}
}
public void sortTitle() {
Collections.sort(songs2, SongComparator.byTitle()); // now we have a sorted list
}
public void sortRank() {
Collections.sort(songs2, SongComparator.byRank()); // now we have a sorted list
}
public void sortArtist() {
Collections.sort(songs2, SongComparator.byArtist()); // now we have a sorted list
}
public void sortYear() {
Collections.sort(songs2, SongComparator.byYear()); // now we have a sorted list
}
public String toString() {
String result = "";
for (int i = 0; i < songs2.size(); i++) {
result += " " + songs2.get(i);
}
return result;
}
}
导入java.io.File;
导入java.io.FileNotFoundException;
导入java.util.*;
公营歌曲集{
ArrayList歌曲2;
ArrayList itemsToRemove=新建ArrayList();//第二个集合
//对于要删除的项目
//除去
公共歌曲集(ArrayList歌曲){//SongCollection的构造函数
系统输出打印(“测试”);
this.songs2=歌曲;
}
公共空过滤器耳(范围r){
int n=0;
如果(n==0){
System.out.println(“程序正在处理”);
n++;
for(歌曲1:songs2){
如果(宋1.year>(r.getMax())| |(宋1.year)<(r.getMin()){
itemsToRemove.add(song1);
}
}
歌曲2.removeAll(itemsToRemove);
itemsToRemove.clear();
}
}
公共空过滤器(范围r){
int n=0;
如果(n==0){
System.out.println(“程序正在处理”);
n++;
for(歌曲1:songs2)
import java.util.Comparator;
public class SongComparator implements Comparator<Song> {
public enum Order{
YEAR_SORT, RANK_SORT, ARTIST_SORT, TITLE_SORT
}
private Order sortingBy;
public SongComparator(Order sortingBy){
this.sortingBy = sortingBy;
}
public static SongComparator byTitle() {
return new SongComparator(SongComparator.Order.TITLE_SORT);
}
public static SongComparator byYear() {
return new SongComparator(SongComparator.Order.YEAR_SORT);
}
public static SongComparator byArtist() {
return new SongComparator(SongComparator.Order.ARTIST_SORT);
}
public static SongComparator byRank() {
return new SongComparator(SongComparator.Order.RANK_SORT);
}
@Override
public int compare(Song song1, Song song2) {
switch (sortingBy) {
case YEAR_SORT:
System.out.println("test");
return Integer.compare(song1.year, song2.year);
case RANK_SORT:
System.out.println("test");
return Integer.compare(song1.rank, song2.rank);
case ARTIST_SORT:
System.out.println("test");
return song1.artist.compareTo(song2.artist);
case TITLE_SORT:
System.out.println("test");
return song1.title.compareTo(song2.title);
}
throw new RuntimeException(
"Practically unreachable code, can't be thrown");
}
}
while (input.hasNextLine()) {
// stuff happens here
System.out.println(collection.toString());
/*
* System.setOut(console); //changes output to print back to console Scanner outputFileScanner = new Scanner(outputFile); //inputs data from file while ((outputFileScanner.hasNextLine()))
* { //while the file still has data System.out.println(outputFileScanner.nextLine()); //print } outputFileScanner.close(); out.close();
*/
}