Results 1 to 18 of 18
Like Tree2Likes
  • 2 Post By Dark

Thread: beginer java question on arrays

  1. #1
    manny1 is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default beginer java question on arrays

    i just started java and programming in general. im using head first java. theres this one part of code dont understand.

    int x = 0;
    while (x < myDogs.length){
    myDogs[x].bark ();
    x = x + 1
    }

    i know this is easy but what does the [x] mean
    Last edited by manny1; 07-06-2011 at 08:45 PM.

  2. #2
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    x is a variable. It is declared in this statement.
    Java Code:
    int x = 0;
    Read this article: Variables (The Java Tutorials > Learning the Java Language > Language Basics)

    Read it until you finish the Variable Summary page, that will explain everything. Its a few pages long, so it should keep you busy for a little while.
    sunde887 and umairrockx like this.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  3. #3
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    In addition to the Post by Dark: the brackets around the variable [x] mean that the variable is actually the index of the array, or in easier terms, the "place" in the array. In the code snippet you got there, you're setting the variable x = 0; so when you then reference [x] that means you are at the first index of the array (since indices start at 0, not 1). Hope this helps! I started programming a few years ago myself, and it is really fun. Keep at it, and read APIs and visit this forum as much as you can!

  4. #4
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Ah yes, how could I have forgotten to mention that. Good catch. The Dogs variable is an array, you can tell because of the brackets. Now we don't see where Dogs is declared, so we do not know how big the Dogs array is. However all arrays will have an array 0 with a declared size or variable(s), we assume that the Dogs array declares a size. In this case x represents 0 and will show what object reference is in the Dogs array at location 0.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  5. #5
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    Yep, there are a few important declarations missing so yes, we'll just assume your code compiles. What happens when Dogs[x].bark()? Have you created the bark() class?

    Also, for the sake of readability, use a 'for' loop to manipulate the array like this:

    for(int x = 0; x < Dogs.length(); x++)
    {

  6. #6
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    Sorry accidentally hit send on that one. Here you go:

    Java Code:
    for (int x = 0; x <Dogs.length(); x ++){
       Dogs[x].bark(); }

  7. #7
    manny1 is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    thanks the dog arrays length was 3 and yea the bark class was already created. it was just some code found in head first java. thanks i think got it

  8. #8
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    I suggest reading the entire book, I just finished that book and it was by far my favorite introductory programming book I've ever read.

    The code changed a bit from his original code, but either way.
    Original was:
    Java Code:
    int x = 0;
    while (x < myDogs.length){
    myDogs[x] = new Dog();
    }
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  9. #9
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    I love Java For Dummies by Doug Lowe All-In-One-desk reference. Great value, and it covers everything in a very self-explanatory way.

  10. #10
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Quote Originally Posted by Joel View Post
    I love Java For Dummies by Doug Lowe All-In-One-desk reference. Great value, and it covers everything in a very self-explanatory way.
    I'll keep this in mind, when I get back to the States I plan on buying a few more books and I think the best way to choose a book is by word of mouth. That's how I found Head First Java.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  11. #11
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    So Dark, are you in the Marine Corps?

  12. #12
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Yes, as stated in my Bio. However this is rather off topic. If you'd prefer to discuss something about me, feel free to PM me. As long as its in good taste and not offensive, I will entertain it.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  13. #13
    Joel's Avatar
    Joel is offline Member
    Join Date
    May 2011
    Location
    Placerville, CA
    Posts
    86
    Rep Power
    0

    Default

    cool. The best way to get a hold of me is at Skype. My contact ID is ilovespanish2007. ttyl.

  14. #14
    Junky's Avatar
    Junky is offline Grand Poobah
    Join Date
    Jan 2011
    Location
    Dystopia
    Posts
    3,779
    Rep Power
    7

    Default

    Quote Originally Posted by Dark View Post
    all arrays will have an array 0
    [nitpick]
    No they wont.
    Java Code:
    int[] arr = int[0];
    System.out.println(arr[0]); //KABOOOM!

  15. #15
    manny1 is offline Member
    Join Date
    Jul 2011
    Posts
    3
    Rep Power
    0

    Default

    thanks guy if i have any more questions i know have a place to ask

  16. #16
    sunde887's Avatar
    sunde887 is offline Moderator
    Join Date
    Jan 2011
    Location
    Richmond, Virginia
    Posts
    3,069
    Blog Entries
    3
    Rep Power
    8

    Default

    The reason why arrays start at 0 is that it is an offset, basically, how far away from the first element you are. If you are at the first element, you are zero spaces away, hence index 0, and so on for all the elements in the array.

    One thing to keep in mind with head first java is that many of the examples are kind of "theoretical", and not necessarily compilable, try your best to understand them and don't hesitate to ask questions.
    Last edited by sunde887; 07-07-2011 at 04:02 AM.

  17. #17
    Dark's Avatar
    Dark is offline Senior Member
    Join Date
    Apr 2011
    Location
    Camp Lejuene, North Carolina
    Posts
    643
    Rep Power
    4

    Default

    Quote Originally Posted by Junky View Post
    [nitpick]
    No they wont.
    Java Code:
    int[] arr = int[0];
    System.out.println(arr[0]); //KABOOOM!
    Ok, I don't consider 0 a size since it represents nothing. I see your point though.
    • Use [code][/code] tags when posting code. That way people don't want to stab their eyes out when trying to help you.
    • +Rep people for helpful posts.

  18. #18
    atheeth is offline Member
    Join Date
    Jul 2011
    Posts
    1
    Rep Power
    0

    Default

    x is a variable...but when an [x] is used it reffered to as variable index of an array.which may start from zero...

Similar Threads

  1. question about 2d arrays
    By jaxber in forum New To Java
    Replies: 2
    Last Post: 06-03-2011, 04:02 PM
  2. Java question - arrays
    By encyclopedia in forum New To Java
    Replies: 8
    Last Post: 09-15-2010, 12:48 PM
  3. Java arrays (not sure if this question is advanced)
    By aga010607 in forum Advanced Java
    Replies: 1
    Last Post: 11-24-2009, 01:43 PM
  4. Question about 2D arrays
    By fanle in forum New To Java
    Replies: 7
    Last Post: 07-22-2009, 05:52 PM
  5. question about arrays
    By broganm1 in forum New To Java
    Replies: 3
    Last Post: 02-13-2008, 02:29 AM

Posting Permissions

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