首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
社区首页 >问答首页 >此程序是否需要多个ArrayLists?

此程序是否需要多个ArrayLists?
EN

Stack Overflow用户
提问于 2017-05-06 09:03:12
回答 2查看 57关注 0票数 0

我必须编写一个程序来读取最喜欢的文件,比如最喜欢的动物。假设它在一个文件中读取"Dog,Cat,Fox",每行1个。然后将它们放入数组列表中。在提示用户添加或删除任何内容后,系统会要求他们对这些内容进行排名。这就是我困惑的地方--为了重新排序数组列表中的行,我需要第二个arraylist来放入排名吗?此外,在他们被排名后,他们被要求为每个人添加一个评论,比如对猫的评论-“我对猫过敏”或类似的东西。我需要这些评论的第三个数组列表吗?这是一个示例输出-

‘’Favorite‘| Rank |本轮评论(加上所有以前的评论)

这是我现在的代码,如果你想知道我要去哪里--它不能全部工作,但你会明白它的要点的

代码语言:javascript
代码运行次数:0
运行
复制
 Scanner input = new Scanner(System.in);
        ArrayList <String> favoriteAnimals = new ArrayList <String>();
        boolean repeat = true;
        while (repeat) {

            System.out.println("Enter the name of the file which contains your favorite animals ");
            String fileName = input.nextLine().trim();

            try (BufferedReader reader = new BufferedReader(new FileReader(fileName))) {

                String line;
                System.out.println("Here are your favorite animals according to the file:");  
                 while ((line = reader.readLine()) != null) {    
                   System.out.println(line);   
                   favoriteAnimals.add((line));
                 }                                                

                 System.out.println("Add more? (y/n)");
                 if (input.next().startsWith("y")) {
                     System.out.println("Enter : ");
                     favoriteAnimals.add(input.next());
                 } else {
                    break;
                 }
                 for (int i = 0; i < favoriteAnimals.size(); i++) {
                     System.out.println(favoriteAnimals);
                 }

                     System.out.println("Remove an animal?");

                     if (input.next().startsWith("y")) {
                         System.out.println("Which animal would you like to remove");
                            String removeAnimal = input.nextLine();
                            int index = favoriteAnimal.indexOf(removeAnimal);
                            favoriteAnimals.remove(index);
                            System.out.println(favoriteAnimals);
                     }
                     else {
                         break;
                     }

                     ArrayList <String> ranking = new ArrayList <String>();
                    int size = favoriteAnimals.size();
                     String first = favoriteAnimals.get(0);
                    System.out.println("What is your ranking of " + first + " out of " + size +"?");                 


            }



            catch (IOException ex) {
                System.err.println("Your file does not exist!: " + ex.getLocalizedMessage());
                repeat = true;
            }
        }
    }
}
EN

回答 2

Stack Overflow用户

发布于 2017-05-06 09:36:02

很有可能,您应该编写一个包含String nameString comment等字段的小class Animal,然后拥有一个ArrayList<Animal>

票数 1
EN

Stack Overflow用户

发布于 2017-05-06 09:57:45

如果你熟悉面向对象的面向对象,那么建议使用类来封装你已有的数据结构,我写了一个简单的例子,希望它能帮助你解决这个问题:

动物类

代码语言:javascript
代码运行次数:0
运行
复制
package com.stackoverflow.q1;

import java.util.ArrayList;
import java.util.List;

public class Animal {

private String name;
private Integer rank;
private List<String> comments=new ArrayList<String>();

public String getName() {
    return name;
}
public void setName(String name) {
    this.name = name;
}
public Integer getRank() {
    return rank;
}
public void setRank(Integer rank) {
    this.rank = rank;
}
public List<String> getComments() {
    return comments;
}
public void setComments(List<String> comments) {
    this.comments = comments;
}
@Override
public String toString() {
    return "Animal [name=" + name + ", rank=" + rank + ", comments="
            + comments + "]";
}
}

主类

代码语言:javascript
代码运行次数:0
运行
复制
package com.stackoverflow.q1;

import java.util.ArrayList;
import java.util.List;

public class Main {

public static void main(String[] args) {

    List<Animal> animals= new ArrayList<Animal>();

    Animal dog=new Animal();
    dog.setName("dog");
    dog.setRank(1);
    dog.getComments().add("Comment 1");

    Animal horse=new Animal();
    horse.setName("horse");
    horse.setRank(2);
    horse.getComments().add("Comment 1");


    Animal cow=new Animal();
    cow.setName("cow");
    cow.setRank(1);
    cow.getComments().add("Comment 1");

    animals.add(dog);
    animals.add(horse);
    animals.add(cow);


    for(Animal animal:animals){
        System.out.println(animal);
    }

}

}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/43815659

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档