【问题标题】:int from ArrayList to use in a different class来自 ArrayList 的 int 在不同的类中使用
【发布时间】:2016-08-25 05:38:37
【问题描述】:

我想做的是做一些我可以用来计划我一年中工作量的东西。我正在编写一个程序,在其中手动输入班级名称、页数和班级难度。

我正在使用一个类,其中我放入了我之前命名的所有数据,另一个类负责计算工作。

包含大学课程信息的Java类:

    public classInfo(String className, int classAmount, int classDifficulty, int classTime)
    {
        name = className; // name of the class
        amount = classAmount; // amount of pages
        difficulty = classDifficulty; // difficulty (1 = easy >> 3 = hard)
        time = classTime; // time you have (1 or 2 semesters)
    }

    /**
     * Prints all the data you have put in about this class
     */
    public void printInfo()
    {
        System.out.println("Vak: " + name);
        System.out.println("Hoeveelheid leerstof: " + amount + " pagina's");
        System.out.println("Moeilijkheidsgraad: " + difficulty);
        System.out.println("Dit vak duurt: " + time + " semester(s)");
    }

    public String toString() 
    {
    return naam;
    }
}

我测试了这些方法,它们都按我的预期工作。最后一个方法 (toString() ) 是我创建的一个方法,以便我可以打印我保存在第二个类的 ArrayList 中的所有类的列表。应该进行计算的那个。

这是我的第二堂课的概述:(应该进行计算的那个)

import java.util.ArrayList;
public class Planner
{
    private ArrayList<ClassInfo> course;
    private int index;

    /**
     * constructor
     */
    public Planner()
    {
        course = new ArrayList<ClassInfo>();
    }

    /**
     * This method is used to add classes to the ArrayList
     */
    public void addClass(ClassInfo aCourse)
    {  
        course.add(aCourse);
    }

    /**
     * With this method you can print all the names of the classes that are currently in the ArrayList
     */
    public void showClass()
    {
        for(index = 0; index < course.size(); index++)
        {
            System.out.println(course.get(index));
        }
    }

    /**
     * This method is used to calculate the workload of the classes.
     */
    public void berekenKlas()
    {
       /////// HERE IS MY PROBLEM.   /////
    }
}

我想知道如何告诉 Java 我保存在 ArrayList 中的不同整数之间的区别。

我想要做的是循环遍历所有类(就像在循环遍历类名称的方法中一样)并将“页数”和“时间”的数据保存到 2 个不同的变量中。

我目前的困惑是我不知道该怎么做。谁有想法?

提前谢谢你。

【问题讨论】:

  • 既然你已经有了一个循环,你显然知道如何提取值。你想从那里做什么? “保存数据”是什么意思?
  • 你可以有两种方法来生成你需要的两条数据public int totalTime() { int result=0; for(index = 0; index &lt; course.size(); index++) { result+=course.get(index).time; } return result; } public int totalDifficulty() { int result=0; for(index = 0; index &lt; course.size(); index++) { result+=course.get(index).difficulty; } return result; }
  • 我真的只是想弄清楚如何告诉 Java 我想要使用的 2 个整数之间的差异。两个整数都保存在同一个 ArrayList 中。

标签: java arraylist bluej


【解决方案1】:

首先,您可以为循环使用 foreach 构造:

public void showClass() {
    for(ClassInfo courseItem: course) {
        System.out.println(course);
    }
}

但我不明白你的问题。您已经在使用字段来分隔 ArrayList 的 ClassInfo 类型中的不同整数。

要保存这两个值,您可以创建另一个类,具体取决于您想对这些值做什么。

【讨论】:

    【解决方案2】:

    在您的 ArrayList 中,您有 ClassInfo 对象实例,而不是整数。 每个 ClassInfo 实例包含名称、数量、难度、时间属性。

    您可以使用 .操作员。例如:

    public void showClass() {
        for (ClassInfo classInfo:course) {
            System.out.println("Name of class:"+classInfo.name);
            System.out.println("Time needed:"+classInfo.time+" semesters");
            // and so on
        }
    }
    

    注意 for-each 循环的使用(它就像 10 多年前的 Java 构造,请咨询您的导师或互联网)。我发现考虑使用标准缩进并遵守 Java 编码标准会让生活更轻松 :)

    【讨论】:

    • 非常感谢 Gee Bee!很有帮助!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-05-28
    • 2020-10-02
    • 2013-06-15
    • 2015-02-02
    • 2016-12-23
    相关资源
    最近更新 更多