Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/352.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何标记循环索引?_Java_Loops - Fatal编程技术网

Java 如何标记循环索引?

Java 如何标记循环索引?,java,loops,Java,Loops,嗨,我想标记一个已经访问过的职位 这根绳子 String s = "123+4+3233" 和多重索引 所以我将循环它必须是内部循环 我只是想 预期结果 您不必标记访问过的地点,只需推进i索引: public static void main(String[] args) { String s = "123+4+3233"; String n = ""; int count = 0; List<

嗨,我想标记一个已经访问过的职位

这根绳子

String s = "123+4+3233" 
和多重索引

所以我将循环它必须是内部循环

我只是想

预期结果


您不必标记访问过的地点,只需推进
i
索引:

 public static void main(String[] args) {
    String s = "123+4+3233";
    String n = "";
    int count = 0;
    List<String> lists = new ArrayList<>();
    for(int i=0; i < s.length(); i++){
        if(s.charAt(i) > 0){
            int k = i;
            for(; k < s.length(); k++){
                if(s.charAt(k) == '+'){
                    break;
                }
                if(s.charAt(k) != '+'){
                    n += s.charAt(k);
                }
            }
            if(!(n.isEmpty())){
                lists.add(n);
                n = "";
            }
            if(s.charAt(i) == '+'){count++; }

            if(k > i)i = k - 1; //advance i to the right position
        }
   }
    System.out.println(lists);
}

我觉得你把这件事弄得有点复杂了。下面是我将如何解决它:

String s = 123+4+3233;
ArrayList<String> nums = new ArrayList<>();

int lastPlus = 0; // Keeping track of the last index at which there was a "+"

for (int j = 0 j < s.length(); j++) {
    if (s.charAt(j).equals("+")) {
        nums.add(s.substring(lastPlus, j));
        lastPlus = j + 1; //The "+1" skips over the plus for the next substring()
    }

    // This last 'if' statement is for adding that last bit of numbers
    if (j == s.length() - 1 && !(s.charAt(j).equals("+"))) { 
        nums.add(lastPlus);
    }
}
字符串s=123+4+3233;
ArrayList nums=新的ArrayList();
int lastPlus=0;//跟踪最后一个有“+”的索引
对于(int j=0 j

这应该行得通。

我认为可以用更短的方式来完成:

public static void main(String... args){
    String s = "123+4+3233";
    List<String> lists = new ArrayList<>();
    for(int i = 0; i<s.length();i++){
        String temp = "";
        while(i<s.length() && s.charAt(i) != '+'){
            temp+= s.charAt(i);
            i++;
        }
        lists.add(temp);
    }
    System.out.println(lists);
}
publicstaticvoidmain(字符串…参数){
字符串s=“123+4+3233”;
List lists=新建ArrayList();

对于(int i=0;我可以使用regexp吗?no bro不能使用它,因为它是编译器的一部分为什么不能使用split()?如果您只需要[123,4323],那么请执行
s.split(+));
。嘿,兄弟,如果主循环中有更多的循环,我会遇到一些问题。例如,如果字符串到达末尾时没有停止,并且一直在减法。@realname没有真正理解你,你是说当它到达末尾时,重新启动for循环以处理另一个新字符串?或者说你有多个字符串要处理如果这就是你的意思,你可以把
main
中的所有代码放到另一个静态方法中,例如,
process(String str)
,然后将
main
重写为`for(int i=0;ii){i=k-1;}当然,请考虑每次内部for循环何时停止?它必须是
+
字符或字符串的结尾,对吗?因此对于满足
+
字符的第一种情况,这意味着您可以在该字符串“123+4+3233”中找到一个数字,当您在索引3处遇到第一个
+
字符时,您将第一个数字
123
保存在列表中,然后开始查找下一个数字,然后必须从索引3开始(此时k==3)因为你当时正在计算,但为什么i=k-1,这是因为在外for循环中你做i++,这有点平衡,让我在下一个循环中从k开始。
 public static void main(String[] args) {
    String s = "123+4+3233";
    String n = "";
    int count = 0;
    List<String> lists = new ArrayList<>();
    for(int i=0; i < s.length(); i++){
        if(s.charAt(i) > 0){
            int k = i;
            for(; k < s.length(); k++){
                if(s.charAt(k) == '+'){
                    break;
                }
                if(s.charAt(k) != '+'){
                    n += s.charAt(k);
                }
            }
            if(!(n.isEmpty())){
                lists.add(n);
                n = "";
            }
            if(s.charAt(i) == '+'){count++; }

            if(k > i)i = k - 1; //advance i to the right position
        }
   }
    System.out.println(lists);
}
[123, 4, 3233]
String s = 123+4+3233;
ArrayList<String> nums = new ArrayList<>();

int lastPlus = 0; // Keeping track of the last index at which there was a "+"

for (int j = 0 j < s.length(); j++) {
    if (s.charAt(j).equals("+")) {
        nums.add(s.substring(lastPlus, j));
        lastPlus = j + 1; //The "+1" skips over the plus for the next substring()
    }

    // This last 'if' statement is for adding that last bit of numbers
    if (j == s.length() - 1 && !(s.charAt(j).equals("+"))) { 
        nums.add(lastPlus);
    }
}
public static void main(String... args){
    String s = "123+4+3233";
    List<String> lists = new ArrayList<>();
    for(int i = 0; i<s.length();i++){
        String temp = "";
        while(i<s.length() && s.charAt(i) != '+'){
            temp+= s.charAt(i);
            i++;
        }
        lists.add(temp);
    }
    System.out.println(lists);
}