views:

135

answers:

4

Related question, about assignment-initialization-declaration.

$ javac MatchTest.java 
MatchTest.java:7: ')' expected
  for((int i=-1 && String match="hello"); (i=text.indexOf(match)+1);)
          ^
MatchTest.java:7: ';' expected
  for((int i=-1 && String match="hello"); (i=text.indexOf(match)+1);)
                         ^
MatchTest.java:7: ';' expected
  for((int i=-1 && String match="hello"); (i=text.indexOf(match)+1);)
                                       ^
MatchTest.java:7: not a statement
  for((int i=-1 && String match="hello"); (i=text.indexOf(match)+1);)
                                          ^
MatchTest.java:7: illegal start of expression
  for((int i=-1 && String match="hello"); (i=text.indexOf(match)+1);)
                                                                    ^
5 errors
$ cat MatchTest.java 
import java.util.*;
import java.io.*;

public class MatchTest {
 public static void main(String[] args){
  String text = "hello0123456789hello0123456789hello1234567890hello3423243423232";
  for((int i=-1 && String match="hello"); (i=text.indexOf(match)+1);)
   System.out.println(i);
 }
}
+3  A: 

You should do it with ",". But only from the same type.

For example:

for ( int i=0, j= 0 ; i < something ; i++, j++)

You can't do something like:

for ( int i=0, String j= "hi" ; i < something ; i++)
Macarse
Doesn't work.Multiple markers at this line - Syntax error on token "int", delete this tokenBut if you remove the second int, it will.
Chris Dennett
@Chris: Thanks, I just fixed my answer.
Macarse
+1  A: 

You can't declare and initialize a variable of a second type, but you can have multiple variables (possibly with initialization) of the same type.

This is a common idiom of caching a for bound in a local variable:

String s = "Hello";
for (int i = 0, L = s.length(); i < L; i++) {
    System.out.println(s.charAt(i));
} // prints "H", "e", "l", "l", "o"

This is a common idiom for mapping a 1D-2D array:

int[] arr1d = new int[9];
int[][] arr2d = {
    { 1, 2, 3 },
    { 4, 5, 6 },
    { 7, 8, 9 },
};
for (int r = 0, i = 0; r < 3; r++) {
    for (int c = 0; c < 3; c++) {
        arr1d[i++] = arr2d[r][c];
    }
}
System.out.println(Arrays.toString(arr1d));
// prints "[1, 2, 3, 4, 5, 6, 7, 8, 9]"

This idiom of two iterators coming in from both ends is also common:

boolean isPalindrome(String s) {
    for (int i = 0, j = s.length() - 1; i < j; i++, j--) {
        if (s.charAt(i) != s.charAt(j)) {
            return false;
        }
    }
    return true;
}
polygenelubricants
Tricky assignment "arr1d[i++] = arr2d[r][c];". "i++" apparently means "do assignment first" then the increase bacause it will get an error otherwise, see reciting reply.
HH
+2  A: 

Its not very clear what you want to do. But I guess you are trying to get all the occurrences of the string "hello" in your bigger string. If so, you can try:

String text = "hello0123456789hello0123456789hello1234567890hello3423243423232";
String match="hello";
for(int i=0; (i = text.indexOf(match,i)) != -1;i += match.length()){    
    System.out.println(i);      
}

Output:

0
15
30
45

I've used the overloaded version of indexOf which takes the index to start the search from as an argument.

codaddict
A: 

polygenelubricants's reply contains noteworthy points. If you change i++ to ++i, the increase-assignment order changes and you can easily get an error.

$ javac ThirdDArray.java 
$ java ThirdDArray      
Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 9
 at ThirdDArray.main(ThirdDArray.java:17)
$ cat ThirdDArray.java 

import java.util.*; 
import java.io.*; 

public class ThirdDArray { 


        public static void main(String[] args) { 

                int[] arr1d = new int[9]; 
                int[][] arr2d = { 
                        { 1, 2, 3 }, 
                        { 4, 5, 6 }, 
                        { 7, 8, 9 }, 
                }; 
                for (int r = 0, i = 0; r < 3; r++) { 
                        for (int c = 0; c < 3; c++) { 
                                //THIS IS THE TRICK POINT!  
                                // ++i means "first increase then assignment" 
                                // i++ means "first assignment then increase" 
                                arr1d[++i] = arr2d[r][c]; 
                        } 
                } 

                // ERROR ONLY DUE TO THE ASSIGNMENT-INCREASE order! 
                System.out.println(Arrays.toString(arr1d)); 
                // started from 1 so go out of the allocated size 
        } 
}
HH
The issue is post-increment vs pre-increment. See also: http://stackoverflow.com/questions/2377145/unable-to-make-out-this-assignment-in-java/2377155#2377155
polygenelubricants