Java 如何从LinkedList获取数据?

Java 如何从LinkedList获取数据?,java,linked-list,Java,Linked List,下面是我正在为我的程序寻找的一个示例输出 示例输出 在每行输入体育场名称和比赛收入 完成后输入done 巨人1000 福克斯博罗500 巨人1500 完成 输入体育场的名称以获取以下项目的总收入: 巨人 总收入为2500.00美元 除了最后的总收入,我什么都做了。我不知道如何进入我的linkedlist,获取游戏收入并展示它。。这是我现在的代码,我一直在搞乱 import java.util.LinkedList; import java.util.Scanner; public class

下面是我正在为我的程序寻找的一个示例输出

示例输出

在每行输入体育场名称和比赛收入

完成后输入done

巨人1000

福克斯博罗500

巨人1500

完成

输入体育场的名称以获取以下项目的总收入:

巨人

总收入为2500.00美元

除了最后的总收入,我什么都做了。我不知道如何进入我的linkedlist,获取游戏收入并展示它。。这是我现在的代码,我一直在搞乱

import java.util.LinkedList;
import java.util.Scanner;


public class LinkedLists {

    private final Scanner keyboard;



    private final LinkedList<String> stadiumNames;
    private final LinkedList<Integer> gameRevenue;


    public LinkedLists() {
        this.keyboard = new Scanner(System.in);
        this.stadiumNames = new LinkedList<String>();
        this.gameRevenue = new LinkedList<Integer>();
    }

    public void addData(String stadium, int revenue){
        stadiumNames.add(stadium);
        gameRevenue.add(revenue);
    }

    public void loadDataFromUser() {
        System.out.println("On each line enter the stadium name and game revenue.");
        System.out.println("Enter done when you are finished.");

        boolean done = false;
        while(!done) {
            System.out.print("Enter the name of the stadium:");
            String stadium = keyboard.next();
            if (stadium.equals("done")) {
                done = true;
            } else {
                System.out.print("Enter game revenue: ");
                addData(stadium, keyboard.nextInt());
            }
        }
    }

    // return -1 if not found
    public int getIndexForName(String name) {

        if(stadiumNames.contains(name)){
            System.out.println("The total revenue is: " +gameRevenue.get(0));
            System.exit(0);
        }

        return -1;

    }


    public void showInfoForName(String name) {
        int index = getIndexForName(name);
        if (index==-1) {
            System.out.println("There is no stadium named " + name);
        } else {

        }
    }

    public void showInfoForName() {
        System.out.println("Enter the name of the stadium to get the total revenue for it.");
        showInfoForName(keyboard.next());
    }

    public static void main(String[] args) {
        LinkedLists pgm = new LinkedLists();
        pgm.loadDataFromUser();
        pgm.showInfoForName();
    }
}
import java.util.LinkedList;
导入java.util.Scanner;
公共类链接列表{
专用最终扫描仪键盘;
私人最终LinkedList体育场名称;
私人最终LinkedList游戏收入;
公共链接列表(){
this.keyboard=新扫描仪(System.in);
this.statiumnames=新建LinkedList();
this.gameRevenue=新建LinkedList();
}
公共无效添加数据(字符串体育场,国际收入){
体育场名称。添加(体育场);
游戏收入。添加(收入);
}
public void loadDataFromUser(){
System.out.println(“在每行输入体育场名称和比赛收入”);
System.out.println(“完成后输入done”);
布尔完成=假;
而(!完成){
System.out.print(“输入体育场名称:”);
String stadium=keyboard.next();
如果(等于(“完成”)){
完成=正确;
}否则{
System.out.print(“输入游戏收入:”);
addData(体育场,键盘.nextInt());
}
}
}
//如果未找到,返回-1
public int getIndexForName(字符串名称){
if(statiumnames.contains(name)){
System.out.println(“总收入为:+gameRevenue.get(0));
系统出口(0);
}
返回-1;
}
public void showInfoForName(字符串名称){
int index=getIndexForName(名称);
如果(索引==-1){
System.out.println(“没有名为“+name”的体育场);
}否则{
}
}
public void showInfoForName(){
System.out.println(“输入体育场名称以获取其总收入”);
showInfoForName(keyboard.next());
}
公共静态void main(字符串[]args){
LinkedList pgm=新LinkedList();
pgm.loadDataFromUser();
pgm.showInfoForName();
}
}
试试这个:

public int getIndexForName(String name) {
    if (stadiumNames.contains(name)) {
        int total = 0;
        for (int i = 0 ; i < stadiumNames.size() ; i++)
            if (stadiumNames.get(i).equals(name))
                total += gameRevenue.get(i);
        System.out.println("The total revenue is: " + total);
        System.exit(0);
    }
    return -1;
}
public int getIndexForName(字符串名称){
if(statiumnames.contains(name)){
int-total=0;
对于(int i=0;i

不过,有更好的方法可以做到这一点。我可能会使用一个
将每个体育场名称映射到其总收入。您可以在
addData
方法中更新此映射。

使用哈希映射更为简洁,此外,您还可以将总数保存在一边,而不计算:

import java.util.Scanner;
import java.util.Map;
import java.util.HashMap;

/**
 * User: user
 * Date: 09/09/2012
 */
public class Blah {

    private Scanner keyboard;
    private Map<String, Integer> stadiumRevenues;
    private Integer total = 0;

    public Blah() {
        this.keyboard = new Scanner(System.in);
        this.stadiumRevenues = new HashMap<String, Integer>();
    }

    public void addData(String stadium, int revenue){
        stadiumRevenues.put(stadium, revenue);
        total += revenue;
    }

    public void loadDataFromUser() {
        System.out.println("On each line enter the stadium name and game revenue.");
        System.out.println("Enter done when you are finished.");

        boolean done = false;
        while(!done) {
            System.out.print("Enter the name of the stadium:");
            String stadium = keyboard.next();
            if (stadium.equals("done")) {
                done = true;
            } else {
                System.out.print("Enter game revenue: ");
                addData(stadium, keyboard.nextInt());
            }
        }
    }

    public int getTotal() {
        return total;
    }

    // return -1 if not found
    public int getIndexForName(String name) {

        Integer rev = stadiumRevenues.get(name);

        if(rev != null){
            System.out.println("The total revenue is: " +rev);
            System.exit(0);
        }

        return -1;

    }

    public void showInfoForName(String name) {

        Integer rev = stadiumRevenues.get(name);

        if (rev == null) {
            System.out.println("There is no stadium named " + name);
        } else {

        }
    }

    public void showInfoForName() {
        System.out.println("Enter the name of the stadium to get the total revenue for it.");
        showInfoForName(keyboard.next());
    }

    public static void main(String[] args) {
        Blah pgm = new Blah();
        pgm.loadDataFromUser();
        pgm.showInfoForName();
    }


}
import java.util.Scanner;
导入java.util.Map;
导入java.util.HashMap;
/**
*用户:用户
*日期:2012年9月9日
*/
公开课之类的{
专用扫描仪键盘;
私人地图球场收入;
私有整数合计=0;
公共废话{
this.keyboard=新扫描仪(System.in);
this.statiumRevenues=新HashMap();
}
公共无效添加数据(字符串体育场,国际收入){
体育场收入。put(体育场,收入);
总额+=收入;
}
public void loadDataFromUser(){
System.out.println(“在每行输入体育场名称和比赛收入”);
System.out.println(“完成后输入done”);
布尔完成=假;
而(!完成){
System.out.print(“输入体育场名称:”);
String stadium=keyboard.next();
如果(等于(“完成”)){
完成=正确;
}否则{
System.out.print(“输入游戏收入:”);
addData(体育场,键盘.nextInt());
}
}
}
公共int getTotal(){
返回总数;
}
//如果未找到,返回-1
public int getIndexForName(字符串名称){
整数rev=statiumRevenues.get(名称);
如果(rev!=null){
System.out.println(“总收入为:“+rev”);
系统出口(0);
}
返回-1;
}
public void showInfoForName(字符串名称){
整数rev=statiumRevenues.get(名称);
如果(rev==null){
System.out.println(“没有名为“+name”的体育场);
}否则{
}
}
public void showInfoForName(){
System.out.println(“输入体育场名称以获取其总收入”);
showInfoForName(keyboard.next());
}
公共静态void main(字符串[]args){
Blah pgm=新Blah();
pgm.loadDataFromUser();
pgm.showInfoForName();
}
}

你必须使用链表吗?^哈哈,如果他只是想要一个总收入,那么可以通过迭代链接列表并总结收入来轻松获得。不过,我怀疑他想要这样的东西。如果你能解释如何使用哈希映射或迭代来获得总收入,那会有所帮助。只是学习如何在我的program@MattMcCarthy您是如何尝试遍历列表并对值求和的?有一个Java语言结构可以支持列表上的简单迭代…很高兴我能提供帮助。不要忘记接受;另外,如果你的问题是家庭作业,你可能会想把它标记为家庭作业。