Results 1 to 18 of 18
Thread: beginer java question on arrays
- 07-06-2011, 08:11 PM #1
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
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] meanLast edited by manny1; 07-06-2011 at 08:45 PM.
- 07-06-2011, 08:21 PM #2
x is a variable. It is declared in this statement.
Read this article: Variables (The Java™ Tutorials > Learning the Java Language > Language Basics)Java Code:int x = 0;
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.- 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.
- 07-06-2011, 08:35 PM #3
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!
- 07-06-2011, 08:46 PM #4
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.
- 07-06-2011, 08:56 PM #5
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++)
{
- 07-06-2011, 08:57 PM #6
Sorry accidentally hit send on that one. Here you go:
Java Code:for (int x = 0; x <Dogs.length(); x ++){ Dogs[x].bark(); }
- 07-06-2011, 09:02 PM #7
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
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
- 07-06-2011, 09:05 PM #8
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.
- 07-06-2011, 09:10 PM #9
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.
- 07-06-2011, 09:12 PM #10
- 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.
- 07-06-2011, 09:14 PM #11
So Dark, are you in the Marine Corps?
- 07-06-2011, 09:17 PM #12
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.
- 07-06-2011, 09:22 PM #13
cool. The best way to get a hold of me is at Skype. My contact ID is ilovespanish2007. ttyl.
- 07-07-2011, 12:45 AM #14
- 07-07-2011, 03:48 AM #15
Member
- Join Date
- Jul 2011
- Posts
- 3
- Rep Power
- 0
thanks guy if i have any more questions i know have a place to ask
- 07-07-2011, 03:56 AM #16
- Join Date
- Jan 2011
- Location
- Richmond, Virginia
- Posts
- 3,069
- Blog Entries
- 3
- Rep Power
- 7
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.
- 07-07-2011, 06:40 AM #17
- 07-07-2011, 07:09 AM #18
Member
- Join Date
- Jul 2011
- Posts
- 1
- Rep Power
- 0
Similar Threads
-
question about 2d arrays
By jaxber in forum New To JavaReplies: 2Last Post: 06-03-2011, 04:02 PM -
Java question - arrays
By encyclopedia in forum New To JavaReplies: 8Last Post: 09-15-2010, 12:48 PM -
Java arrays (not sure if this question is advanced)
By aga010607 in forum Advanced JavaReplies: 1Last Post: 11-24-2009, 01:43 PM -
Question about 2D arrays
By fanle in forum New To JavaReplies: 7Last Post: 07-22-2009, 05:52 PM -
question about arrays
By broganm1 in forum New To JavaReplies: 3Last Post: 02-13-2008, 02:29 AM


2Likes
LinkBack URL
About LinkBacks
Reply With Quote

Bookmarks