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

Thread: Java Array Help

  1. #1
    New to the CF scene
    Join Date
    Mar 2014
    Posts
    1
    Thanks
    0
    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:

    Code:
    public class Student { 
    
    private String name; 
    private int[] tests; 
    
    // Default: Name is "" and 3 scores are 0 
    public Student(){ 
    this(""); 
    } 
    
    // 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
    Posts
    1,718
    Thanks
    41
    Thanked 191 Times in 190 Posts
    Edit: Changes in red (missed a few things and incorrect syntax for Java - thanks Fou-Lu)

    Are you talking about doing this?

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

    Links...
    Oracle Documentation
    Example Code
    Last edited by alykins; 03-03-2014 at 09: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
    Location
    Saskatoon, Saskatchewan
    Posts
    16,987
    Thanks
    4
    Thanked 2,660 Times in 2,629 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'); 


  •  

    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
    •