Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-26-2009, 05:28 PM
Member
 
Join Date: Nov 2009
Posts: 2
Rep Power: 0
enzyme is on a distinguished road
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:

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.
Bookmark Post in Technorati
Reply With Quote
  #2 (permalink)  
Old 11-26-2009, 05:30 PM
PhHein's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Germany
Posts: 343
Rep Power: 1
PhHein is on a distinguished road
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
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-26-2009, 05:32 PM
Member
 
Join Date: Nov 2009
Posts: 2
Rep Power: 0
enzyme is on a distinguished road
Default
Jeez.. I feel kinda dumb now.

Think you for the quick reply.
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-26-2009, 05:35 PM
PhHein's Avatar
Senior Member
 
Join Date: Apr 2009
Location: Germany
Posts: 343
Rep Power: 1
PhHein is on a distinguished road
Default
You're welcome.
__________________
Math problems? Call 1-800-[(10x)(13i)^2]-[sin(xy)/2.362x]
The Ubiquitous Newbie Tips
Bookmark Post in Technorati
Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Printing Array elements new person New To Java 2 02-23-2009 09:57 PM
comparing elements in array garyscott101 New To Java 14 12-10-2008 04:01 PM
How to check whether two elements are available in an array? venkatteshb New To Java 8 08-27-2008 11:45 PM
reference to elements in array Igor New To Java 1 12-14-2007 12:56 PM
Help with array of elements zoe New To Java 1 07-24-2007 06:33 PM


All times are GMT +2. The time now is 06:06 PM.



VBulletin, Copyright ©2000 - 2010, Jelsoft Enterprises Ltd.
Content Relevant URLs by vBSEO ©2009, Crawlability, Inc.
Copyright ©2006 - 2007, www.java-forums.org