Go Back   TechArena Community > Software > Software Development
Become a Member!
Forgot your username/password?
Tags Active Topics RSS Search Mark Forums Read

Sponsored Links



JAVA array

Software Development


Reply
 
Thread Tools Search this Thread
  #1  
Old 06-03-2009
Member
 
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.

Reply With Quote
  #2  
Old 06-03-2009
Member
 
Join Date: Apr 2008
Posts: 2,001
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());
}
}
}
Reply With Quote
  #3  
Old 06-03-2009
Member
 
Join Date: May 2008
Posts: 2,293
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
Reply With Quote
Reply

  TechArena Community > Software > Software Development
Tags:



Thread Tools Search this Thread
Search this Thread:

Advanced Search


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


All times are GMT +5.5. The time now is 05:53 AM.