Hello and welcome to our community! Is this your first visit?
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 4 of 4

Thread: Java Array Help

  1. #1
    New to the CF scene
    Join Date
    Mar 2014
    Thanked 0 Times in 0 Posts

    Java Array Help

    Hello fellow programmers,
    I created a program that utilizes arrays to input and read data using four java classes. One class initializes the program the other reads users input, and the other two hold various method and arrays. My new project is to make a transition from arrays to arrayList, and frankly it confuses me allot. I will post only one of the classes that holds the array I'm positive if anyone can help me with one of them I can figure out the rest on my own. You can ignore most of the bulk of the code I only need help on the initialization of arraylists, for example the tests array has to be an arraylist, and then I'm confused on how to input and receive information from that arraylist. So in short I need help with changing all the arrays here to an arraylist.
    Thank you for reading and here is the code:

    public class Student { 
    private String name; 
    private int[] tests; 
    // Default: Name is "" and 3 scores are 0 
    public Student(){ 
    // Name is nm and 3 scores are 0 
    public Student(String nm){ 
    this(nm, 3); 
    // Name is nm and n scores are 0 
    public Student(String nm, int n){ 
    name = nm; 
    tests = new int[n]; 
    for (int i = 0; i < tests.length; i++) 
    tests[i] = 0; 
    // Name is nm and scores are in t 
    public Student(String nm, int[] t){ 
    //name = nm; 
    tests = new int[t.length]; 
    //for (int i = 0; i < tests.length; i++) 
    // tests[i] = t[i]; 
    // Builds a copy of s 
    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; 
    //Returns null if there are no errors else returns 
    //an appropriate error message. 
    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; 

  • #2
    Senior Coder alykins's Avatar
    Join Date
    Apr 2011
    Thanked 203 Times in 202 Posts
    Edit: Changes in red (missed a few things and incorrect syntax for Java - thanks Fou-Lu)

    Are you talking about doing this?

    public class xAm{
    private ArrayList<Integer> tests = new ArrayList<Integer>();
    You can then do things like
    But I am a little unclear as to 'which' arrays you are wanting to convert.

    Oracle Documentation
    Example Code
    Last edited by alykins; 03-03-2014 at 10:00 PM.

    I code C hash-tag .Net
    Reference: W3C W3CWiki .Net Lib
    Validate: html CSS
    Debug: Chrome FireFox IE

  • #3
    God Emperor Fou-Lu's Avatar
    Join Date
    Sep 2002
    Saskatoon, Saskatchewan
    Thanked 2,662 Times in 2,631 Posts
    Unlike C#, int won't work since its primitive in Java and generics must be cast to Object. You'll have to make your array list a type of integer: private ArrayList<Integer> test = new ArrayList<Integer>();.
    Reading and writing are as simple as .get and .set on the object. It implements iterable as well, so you can use it in a for-each syntax loop. You can simply some of your other functions as well, just take a look over at the collections class to see some shortcuts for what you have used.
    PHP Code:
    header('HTTP/1.1 420 Enhance Your Calm'); 
    Been gone for a few months, and haven't programmed in that long of a time. Meh, I'll wing it ;)

  • #4
    New Coder
    Join Date
    Aug 2014
    Thanked 0 Times in 0 Posts
    Arrays permit efficient (constant time) random access but not efficient insertion and deletion of elements. Consequently, arrays are most appropriate for storing a fixed amount of data which will be accessed in an unpredictable fashion. Another advantage of arrays that has become very important on modern architectures is that iterating through an array has good locality of reference, and so is much faster than iterating through (say) a linked list of the same size, which tends to jump around in memory.


    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts