开发者

Java: 2-assignments-2-initializations inside for-loop not allowed?

开发者 https://www.devze.com 2022-12-26 03:44 出处:网络
Related question, about assignment-initialization-declaration. $ javac MatchTest.java MatchTest.java:7: \')\' expected

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);
 }
}


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;
}


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++)


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.


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 
        } 
}
0

精彩评论

暂无评论...
验证码 换一张
取 消