0

**版主能否删除或锁定这个问题,我现在知道答案了,我没有问清楚问题

好的,对不起,我的代码中的所有内容都发布了

import java.util.*;
import java.lang.*;
import java.io.*;

public class MinorAssignment_PartB {

    public static void main(String[] args) throws Exception {
        List<StudentMarks> marks = new ArrayList<StudentMarks>();
        String File = "studentinfo.txt";
        Scanner scan = new Scanner(new File(File));
        scan.useDelimiter(",");//makes the delimiter a comma

        while(scan.hasNext()) {
            marks.add(new StudentMarks(scan.next(), scan.next(),
                            scan.nextDouble(), scan.nextDouble(),
                            scan.nextDouble(), scan.nextDouble()));

            System.out.printf("%-23s %-15s %-15s %-15s " +
                    "%-15s %-15s %-15s %-15s %n", "STUDENT  NAME",
                        "STUDENT FAN", "PART A", "PART B", "PARTt C",
                                            "PART D", "MARK", "GRADE");

            for (int i = 0; i < marks.size(); i++) {
                System.out.println(marks.get(i));
            }
        }

和班级

import java.text.*;

public class StudentMarks {

    //contains a student class and an array of doubles. 
    private Student student = new Student();
    private double marks[] = new double[5];
    DecimalFormat fmt = new DecimalFormat("0.##");

    public StudentMarks(String name, String fan,
            double partA, double partB, double partC, double partD) {
        Student stud = new Student(name, fan);
        this.student = stud;
        this.marks[0] = partA;
        this.marks[1] = partB;
        this.marks[2] = partC;
        this.marks[3] = partD;
        this.marks[4] = ((partA*0.1) + (partB*0.4) + (partC*0.2) + (partD*0.3));
    }

    @Override
    public String toString() {
        return "" + student + "\t" + marks[0] + "\t\t" + marks[1] + "\t\t" + marks[2] + "\t\t" + marks[3] + "\t\t" + fmt.format(marks[4])+"%";
    }
}

所以我需要在arraylist的一行中获取一个特定的元素

我有 10 行文本,每行包含 2 个字符串和 2 个像这样的双精度 Adam Adamson adam0001 85.4 79.8 82.4 86.1 9 多行相同格式不同的名称和数字

我有打印第一行的marks.get(0),但我只需要第三个元素,85.4

感谢大家的尝试,但我想通了,抱歉没有很清楚地问问题

4

3 回答 3

1

首先,您必须添加以下 getter 才能访问marksin StudentMarks

public double[] getMarks() {
    return this.marks;
}

然后,您可以使用以下代码片段来获取值,85.4partA

StudentMarks info = marks.get(0);
double[] marksValue = info.getMarks();
double partA = marksValue[0];

partA是必需的变量85.4,您可以根据需要使用它的值。

于 2013-09-29T13:02:28.487 回答
0

StudentMarks在你的类中定义一个 getter 方法:

public double[] getMarks() {
    return marks;
}

System.out.println(marks.get(i).getMarks()[0]);如果你marks[0]是你需要的价值,就使用它。

于 2013-09-29T13:25:09.107 回答
0

尝试转换

Person person =(person)marks.get(0);
System.out.println(person.StudentMarks[0]);

然后得到你需要的属性

于 2013-09-29T13:08:03.240 回答