i have a character array with 4 indices called
char[] pegs = new char[4];
i want to use a for loop that will go through and perform the same action on each of the indices
however when i type
for(pegs = 0; pegs <=3; pegs++){
//all the code goes in here
}
it tells me that im using incompatible types. So, i realize I'm not that much of a coder ... i guess i can see why it's not working - it's a character array, not int. The question is ... how do i do it then?!
Thanks in advance
how do i use a forloop to increment array indices? In Java
-
- DBB Admiral
- Posts: 1699
- Joined: Thu Mar 23, 2000 3:01 am
why do you want to use chars arrays? Java does have a String object you know.
anyways, you can't increments arrays, which is what pegs++ is doing, but pegs[i++](where i is an int), incrementing the index of the array is perfectly legal.
something like:
read thru the API documentation, you'll lots of nifty stuff.
anyways, you can't increments arrays, which is what pegs++ is doing, but pegs[i++](where i is an int), incrementing the index of the array is perfectly legal.
something like:
Code: Select all
for(int i =0; i<=3; i++){
pegs[i]='l';
...
}
Grendel, Java's Array objects are not the same thing as C/C++ arrays, in that they are not pointers to blocks of memory. They are actual objects (Java.lang.Array). Java will do the int i = 0; in the for loop, but I'm not entirely sure that it has the prefix increment operator (++i). Just as valid to use i++ anyway.
-
- DBB Admiral
- Posts: 1699
- Joined: Thu Mar 23, 2000 3:01 am