Java Forums

Main Menu
Home
Today's Posts
FAQ
Search
Contact Us

Java Network
Linux Archive
Java Tips
Java Tips Blog

Sponsored Links





Welcome to the Java Forums.

You are currently viewing our boards as a guest which gives you limited access to view most discussions and access our other features. By joining our free community, you will:

  • have access to post topics
  • communicate privately with other members (PM)
  • not see advertisements between posts
  • have the possibility to earn one of our surprises if you are an active member
  • access many other special features that will be introduced later.

Registration is fast, simple and absolutely free so please, join our community today!

If you have any problems with the registration process or your account login, please contact us.

Reply
 
LinkBack Thread Tools Display Modes
  #1 (permalink)  
Old 11-14-2007, 05:59 AM
Member
 
Join Date: Nov 2007
Posts: 4
nhlfan is on a distinguished road
Question about arrays
I have a question for arrays.

What does the "x.length" (x is a placeholder) class do exactly. I know that if you use it on a string, suppose the word "cat", it would print out a 3.

However, what does it do / print out when you use it on a number, like "90.999" or simply "1000"??

For example I have this code fragment:

double num[] = UserInput // a user inputs a number
for(int x = 0; x < num.length; x++)
System.out.println(num[x]);

What is exactly is going on in that loop, especially with the num.length part, I just dont seem to understand it with numbers.

Thanks for your help!
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
  #2 (permalink)  
Old 11-14-2007, 06:10 AM
JT4NK3D's Avatar
Member
 
Join Date: Nov 2007
Posts: 50
JT4NK3D is on a distinguished road
I'm not exactly sure about that, but i have a suggestion. if you want it to go through the array as a loop (e.g. array with 10 ints, 1 through ten in seperate array places - yourArray[0] would be 1, yourArray[1] would be 2, etc.) after the array is initialized, you use the enhanced for statement,

for ( int item : yourArray) {

System.out.println("Currently at " + item);

}
by the way, the ( int item : yourArray ) must be written exactly that, except your array name, not yourArray. I'm not sure if you can change the name item.. you could always experiment, but i doubt it.

this will go through each array item and is convention and reccomended for easier code reading. i'm not quite sure what is happening when you are getting an entire array from user input, but if u wanted to use an array to loop, thats how. using the word item in the loop block will return the value of the array item that the loop is currently using for the current iteration.

also, when declaring arrays, it is better to declare with syntax : int[] myArray;, not int myArray[] - as with
public static void main(String[] args), not (String args[]).this way is better coding and convention.

Last edited by JT4NK3D : 11-14-2007 at 06:18 AM. Reason: accidentally posted before finished
Bookmark Post in Technorati
Reply With Quote
  #3 (permalink)  
Old 11-14-2007, 06:16 AM
Member
 
Join Date: Nov 2007
Posts: 4
nhlfan is on a distinguished road
well that makes sense...
But what confuses me is the .length class. What does it do. If you were to use it in that fragment you just gave, what would happend?
Bookmark Post in Technorati
Reply With Quote
  #4 (permalink)  
Old 11-14-2007, 06:22 AM
JT4NK3D's Avatar
Member
 
Join Date: Nov 2007
Posts: 50
JT4NK3D is on a distinguished road
Well, .length (obviously) gives the length, but you want to know what happens if you .length a number? on an array, i think it would return the number of elements/items in the array. on a number, i'm guessing either it would return the number of digits( so int ten = 10; System.out.println(ten.length); would return 2) or it would return a compile time error because it might just be ment to use on strings.
Bookmark Post in Technorati
Reply With Quote
  #5 (permalink)  
Old 11-15-2007, 01:38 PM
Member
 
Join Date: Nov 2007
Posts: 2
nicemothi is on a distinguished road
Motilal D
What is the basic difference between the 2 approaches to exception handling.
1. try catch block and
2. specifying the candidate exceptions in the throws clause?
When should you use which approach?
Bookmark Post in Technorati
Reply With Quote
Sponsored Links
Reply


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

vB 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
Question mark colon operator question orchid Advanced Java 4 11-12-2008 10:08 AM
Arrays bunbun New To Java 1 04-09-2008 04:24 AM
question about arrays broganm1 New To Java 3 02-13-2008 04:29 AM
2D-Arrays kbyrne New To Java 1 02-08-2008 12:08 AM
arrays help Warren New To Java 6 11-23-2007 09:23 PM


All times are GMT +3. The time now is 12:06 AM.


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