Results 1 to 5 of 5
  1. #1
    nhlfan is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default 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!

  2. #2
    JT4NK3D's Avatar
    JT4NK3D is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    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 05:18 AM. Reason: accidentally posted before finished

  3. #3
    nhlfan is offline Member
    Join Date
    Nov 2007
    Posts
    4
    Rep Power
    0

    Default

    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?

  4. #4
    JT4NK3D's Avatar
    JT4NK3D is offline Member
    Join Date
    Nov 2007
    Posts
    50
    Rep Power
    0

    Default

    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.

  5. #5
    nicemothi is offline Member
    Join Date
    Nov 2007
    Posts
    2
    Rep Power
    0

    Default 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?

Similar Threads

  1. Question mark colon operator question
    By orchid in forum Advanced Java
    Replies: 9
    Last Post: 12-19-2010, 09:49 AM
  2. Arrays
    By bunbun in forum New To Java
    Replies: 1
    Last Post: 04-09-2008, 03:24 AM
  3. question about arrays
    By broganm1 in forum New To Java
    Replies: 3
    Last Post: 02-13-2008, 03:29 AM
  4. 2D-Arrays
    By kbyrne in forum New To Java
    Replies: 1
    Last Post: 02-07-2008, 11:08 PM
  5. arrays help
    By Warren in forum New To Java
    Replies: 6
    Last Post: 11-23-2007, 08:23 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
  •