Luffy McDuff
Luffy McDuff

Reputation: 5

trouble with array lists

I am doing a project and instead of using an array, I figured an array list would be better. I know I need to declare the array list and its methods, but I am not too sure where to go from there. Any suggestions? Here's code...

public class Student {

    private String name;
    private int[] tests;

    public Student() {
        this("");
    }

    public Student(String nm) {
        this(nm, 3);
    }

    public Student(String nm, int n) {
        name = nm;
        tests = new int[n];
        for (int i = 0; i < tests.length; i++) {
            tests[i] = 0;
        }
    }

    public Student(String nm, int[] t) {
        tests = new int[t.length];
    }

    public Student(Student s) {
        this(s.name, s.tests);
    }

    public int getNumberOfTests() {
        return tests.length;
    }

    public void setName(String nm) {
        name = nm;
    }

    public String getName() {
        return name;
    }

    public void setScore(int i, int score) {
        tests[i - 1] = score;
    }

    public int getScore(int i) {
        return tests[i - 1];
    }

    public int getAverage() {
        int sum = 0;
        for (int score : tests) {
            sum += score;
        }
        return sum / tests.length;
    }

    public int getHighScore() {
        int highScore = 0;
        for (int score : tests) {
            highScore = Math.max(highScore, score);
        }
        return highScore;
    }

    public String toString() {
        String str = "Name:    " + name + "\n";
        for (int i = 0; i < tests.length; i++) {
            str += "test " + (i + 1) + ":  " + tests[i] + "\n";
        }
        str += "Average: " + getAverage();
        return str;
    }

    public String validateData() {
        if (name.equals("")) {
            return "SORRY: name required";
        }
        for (int score : tests) {
            if (score < 0 || score > 100) {
                String str = "SORRY: must have " + 0 + " <= test score <= " + 100;
                return str;
            }
        }
        return null;
    }
}

Upvotes: 0

Views: 181

Answers (5)

Biman Tripathy
Biman Tripathy

Reputation: 2856

I tried to change the array to arraylist. Reply if this doesn't compile correctly.

import java.util.ArrayList;

public class Student {

private String name;
// private int[] tests;
private ArrayList<Integer> tests;

public Student() {
    this("");
}

public Student(String nm) {
    // this(nm, 3);
    name = nm;
}

/*
 * public Student(String nm, int n) { name = nm; tests = new int[n]; for
 * (int i = 0; i < tests.length; i++) { tests[i] = 0; } }
 */

/*
 * public Student(String nm, int[] t) { tests = new int[t.length]; }
 */

public Student(Student s) {
    this(s.name, s.tests);
}

public Student(String name2, ArrayList<Integer> tests2) {
    name = name2;
    tests = tests2;
}

public int getNumberOfTests() {
    return tests.size();
}

public void setName(String nm) {
    name = nm;
}

public String getName() {
    return name;
}

// public void setScore(int i, int score) {
// tests[i - 1] = score;
public void setScore(int score) {

    tests.add(score);

}

public int getScore(int i) {
    // return tests[i - 1];
    return tests.get(i - 1);
}

public int getAverage() {
    int sum = 0;
    for (int score : tests) {
        sum += score;
    }
    // return sum / tests.length;
    return sum / tests.size();
}

public int getHighScore() {
    int highScore = 0;
    for (int score : tests) {
        highScore = Math.max(highScore, score);
    }
    return highScore;
}

public String toString() {
    String str = "Name:    " + name + "\n";
    for (int i = 0; i < tests.size(); i++) {
        str += "test " + (i + 1) + ":  " + tests.get(i) + "\n";
    }
    str += "Average: " + getAverage();
    return str;
}

public String validateData() {
    if (name.equals("")) {
        return "SORRY: name required";
    }
    for (int score : tests) {
        if (score < 0 || score > 100) {
            String str = "SORRY: must have " + 0 + " <= test score <= "
                    + 100;
            return str;
        }
    }
    return null;
}

public ArrayList<Integer> getTests() {
    return tests;
}

public void setTests(ArrayList<Integer> tests) {
    this.tests = tests;
}

}

Upvotes: 0

I think it could be better to use the stl vector instead make your own arrays

Upvotes: 0

spines1
spines1

Reputation: 1

All of the operations you're using are mirrored in the ArrayList API. One thing that's worth noting is that you cannot declare an ArrayList of primitive types, but for each of the primitive types there exists an Object that is the boxed version of the primative.

The boxed version of int is Integer, so you have

ArrayList<Integer> myList = new ArrayList<Integer>();

From there, you need to look up the methods you would need to use in order to manipulate the array. For example, if you want to add the number 42 to the end of the array, you would say

myList.add(42);

The ArrayList API is located here.

Upvotes: 0

hvgotcodes
hvgotcodes

Reputation: 120308

I figured an array list would be better

Maybe. Maybe not. It depends. Does it look like you would get a benefit in using one based on the ArrayList API?

If your "list" never changes size, and you don't need to find things in it, then an array is just as good.

I know I need to declare the array list and its methods, but I am not too sure where to go from there

You need to create a reference to an instance of an ArrayList. That's as simple as

List<Integer> myList = new ArrayList<Integer>();

in your class declaration. You don't need to "declare its methods". When you have a reference to an object, you can invoke its methods.

Upvotes: 3

Brendan Long
Brendan Long

Reputation: 54312

To use an ArrayList, you just need to declare and instantiate it:

// <SomeObject> tells Java what kind of things go in the ArrayList
ArrayList<SomeObject> aDescriptiveNameHere = new ArrayList<SomeObject>();

// This is also valid, since an ArrayList is also a List
List<SomeObject> list = new ArrayList<SomeObject>();

Then you can add things with the add() method:

// Please don't name your list "list"
list.add(new Thing(1));
list.add(new Thing(2));

You can get something by index (like you would with someArray[index]) as:

list.get(index);
// For example
Thing t = list.get(5);

You probably also need the size().

See the JavaDocs for more info.

Upvotes: 0

Related Questions