Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/325.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 String.equals with for each和for循环 String[]rgb=新字符串[3]; rgb[0]=Integer.toHexString(color.getRed()); rgb[1]=Integer.toHexString(color.getGreen()); rgb[2]=Integer.toHexString(color.getBlue()); 用于(字符串el:rgb) { 如果(el.等于(“0”)) { el=“00”; } } 对于(int i=0;i_Java_For Loop_Foreach - Fatal编程技术网

Java String.equals with for each和for循环 String[]rgb=新字符串[3]; rgb[0]=Integer.toHexString(color.getRed()); rgb[1]=Integer.toHexString(color.getGreen()); rgb[2]=Integer.toHexString(color.getBlue()); 用于(字符串el:rgb) { 如果(el.等于(“0”)) { el=“00”; } } 对于(int i=0;i

Java String.equals with for each和for循环 String[]rgb=新字符串[3]; rgb[0]=Integer.toHexString(color.getRed()); rgb[1]=Integer.toHexString(color.getGreen()); rgb[2]=Integer.toHexString(color.getBlue()); 用于(字符串el:rgb) { 如果(el.等于(“0”)) { el=“00”; } } 对于(int i=0;i,java,for-loop,foreach,Java,For Loop,Foreach,对于每个循环,字符串el实际上不是对数组rgb的引用。它只保存数组中给定索引的值。因此,在这种情况下,必须使用正常的for循环来修改数组的内容;否则el的内容是just覆盖循环的每个迭代 直观的解释: String[] rgb = new String[3]; rgb[0] = Integer.toHexString(color.getRed()); rgb[1] = Integer.toHexString(color.getGreen()); rgb[2] = Integer.toHexSt

对于每个
循环,字符串
el
实际上不是对数组
rgb
的引用。它只保存数组中给定索引的值。因此,在这种情况下,必须使用正常的
for
循环来修改数组的内容;否则
el
的内容是just覆盖循环的每个迭代

直观的解释:

String[] rgb = new String[3];

rgb[0] = Integer.toHexString(color.getRed());
rgb[1] = Integer.toHexString(color.getGreen());
rgb[2] = Integer.toHexString(color.getBlue());

for(String el : rgb)
{
    if(el.equals("0"))
    {
        el = "00";
    }
}

for(int i = 0; i<3; i++)
{
    if(rgb[i].equals("0"))
    {
        rgb[i] = "00";
    }
}

String[]i=新字符串[]{“你好”,“你好”};它不会在foreach循环中“运行false”-当您提问时,请确保检查您所做的断言,通常这样做可以让您自己回答问题。@Shahzeb color是一个保存java.awt类型的变量。Color@pvg是的,退一步质疑你的假设总是好的,谢谢你的提醒!
String[] i = new String[]{"Hi", "Hello"};     <------- Contains two indexes.
for(String str : i){
    str = str + "!";
}