Results 1 to 4 of 4
  1. #1
    enzyme is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default Looping Array Elements

    Hi,
    I'm giving a go at Java and found a problem I'm unable to solve here..

    I want to display all the elements in an array. So, I did this:

    Java Code:
    public class Goober {
    	public static void main(String[] args) {
    		double[] classArray = {23.44, 33.23, 88.66, 34.34, 55.55, 33.44};
    		classArray = new double[5];
    		for (int i = 0; i < classArray.length; i++) {
    			System.out.println(classArray[i]);
    		}
    	}
    }
    The output is this:

    0.0
    0.0
    0.0
    0.0
    0.0

    What is it that I've done wrong?
    I'm using OpenJDK 6 on Arch Linux.

  2. #2
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    10

    Default

    First you initialize the array with six values and in the next line you overwrite the reference with a new double array of size 5. As you don't fill the new array you get the default values.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

  3. #3
    enzyme is offline Member
    Join Date
    Nov 2009
    Posts
    2
    Rep Power
    0

    Default

    Jeez.. I feel kinda dumb now.

    Think you for the quick reply.

  4. #4
    PhHein's Avatar
    PhHein is offline Senior Member
    Join Date
    Apr 2009
    Location
    Germany
    Posts
    1,429
    Rep Power
    10

    Default

    :D You're welcome.
    Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
    The Ubiquitous Newbie Tips

Similar Threads

  1. Printing Array elements
    By new person in forum New To Java
    Replies: 2
    Last Post: 02-23-2009, 09:57 PM
  2. comparing elements in array
    By garyscott101 in forum New To Java
    Replies: 14
    Last Post: 12-10-2008, 04:01 PM
  3. Replies: 8
    Last Post: 08-27-2008, 10:45 PM
  4. reference to elements in array
    By Igor in forum New To Java
    Replies: 1
    Last Post: 12-14-2007, 12:56 PM
  5. Help with array of elements
    By zoe in forum New To Java
    Replies: 1
    Last Post: 07-24-2007, 05:33 PM

Posting Permissions

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