Results 1 to 3 of 3

Thread: JAVA array

  1. #1
    Join Date
    Feb 2009
    Posts
    96

    JAVA array

    how do i declare an array that accepts 5 doubles in which the user places the values into the array via input box.

  2. #2
    Join Date
    Apr 2008
    Posts
    2,005

    Re: JAVA array

    public class TestScoresDemo {


    public static void main(String[] args) {
    // An array with test scores.
    // Notice that element 3 contains an invalid score.
    double[] badScores = {97.5, 66.7, 88.0, 101.0, 99.0 };

    // Another array with test scores.
    // All of these scores are good.
    double[] goodScores = {97.5, 66.7, 88.0, 100.0, 99.0 };

    // Create a TestScores object initialized with badScores.
    try
    {
    TestScores tBad = new TestScores(badScores);
    // The following statement should not execute.
    System.out.println("The average of the bad scores is " +
    tBad.getAverage());
    }
    catch (IllegalArgumentException e)
    {
    System.out.println("Invalid score found.\n" + e.getMessage());
    }

    // Create a TestScores object initialized with goodScores.
    try
    {
    TestScores tGood = new TestScores(goodScores);
    System.out.println("The average of the good scores is " +
    tGood.getAverage());
    }
    catch (IllegalArgumentException e)
    {
    System.out.println("Invalid score found.\n" + e.getMessage());
    }
    }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,297

    Re: JAVA array

    Declare an array variable by specifying the type of data to be stored, followed by square brackets []
    Code:
    dataType[] variableName;
    • you can read the [] as the word "array"


    To declare a variable for an array of integers:
    Code:
    int[] nums;
    • which you can read as "int array nums"


    To declare a variable for an array of String objects:
    Code:
    String[] names;
    • which you can read as "String array names" - the array holds String references


    You may also put the brackets after the variable name (as in C/C++), but that is less clearly related to how Java actually works
    Code:
    int nums[]; // not recommended, but legal

Similar Threads

  1. Array of objects in java
    By Ash maker in forum Software Development
    Replies: 5
    Last Post: 27-02-2010, 02:05 AM
  2. How to convert array to collection in java?
    By Steadfast in forum Software Development
    Replies: 4
    Last Post: 06-02-2010, 04:22 PM
  3. Help with two dimensional array java
    By GlassFish in forum Software Development
    Replies: 3
    Last Post: 30-11-2009, 01:57 PM
  4. Array problems in java
    By NetworkeR in forum Software Development
    Replies: 2
    Last Post: 06-11-2009, 02:36 PM
  5. How to create Generic Array in JAVA
    By Swetlano in forum Software Development
    Replies: 3
    Last Post: 06-02-2009, 07:13 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,714,047,439.31467 seconds with 16 queries