Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.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_Arrays_String_Loops_Arraylist - Fatal编程技术网

如何给java足够的时间给变量赋值?

如何给java足够的时间给变量赋值?,java,arrays,string,loops,arraylist,Java,Arrays,String,Loops,Arraylist,我有一个循环,在循环的末尾,一个String[]被添加到ArrayList(在类中声明为非方法)中,在循环的开头,所述String[]被清除其内容: String[] a = new String[2]; while(true){ a[0] = ""; a[1] = ""; -----some code---- (that adds to a[0] and a[1]) -----some code---- //lets call our ArrayList list

我有一个循环,在循环的末尾,一个
String[]
被添加到
ArrayList
(在类中声明为非方法)中,在循环的开头,所述
String[]
被清除其内容:

String[] a = new String[2];
while(true){
  a[0] = "";
  a[1] = "";
  -----some code----

(that adds to a[0] and a[1])

  -----some code----
  //lets call our ArrayList list
  list.add(a);
}

因此,通常情况下,列表中存储的是一个空的
字符串
。我想这是因为java进入下一步的速度太快了,但我不确定,有什么帮助吗? 以下是我的全部代码:

static ArrayList<String[]> Names = new ArrayList<String[]>();
public static void read(BufferedReader stream){
        String[] aux = new String[2];
        char it = 2;

    try{
        while(it != (char) -1){
            aux[0] = "";
            aux[1] = "";
            it = (char) stream.read();

            while(Character.isLetter(it)){
                aux[0] += it;
                it = (char) stream.read();
            }

            it = (char) stream.read();

            while(Character.isDigit(it)){
                aux[1] += it;
                it = (char) stream.read();
            }
            Names.add(aux);
            stream.read();
        }


    }catch(IOException e){
        System.out.print("IOException(read): ");
        System.err.println(e.getMessage());
    }

}
static ArrayList Names=new ArrayList();
公共静态无效读取(BufferedReader流){
字符串[]aux=新字符串[2];
charit=2;
试一试{
while(it!=(char)-1){
辅助[0]=“”;
辅助[1]=“”;
it=(char)stream.read();
while(Character.isleter(it)){
aux[0]+=it;
it=(char)stream.read();
}
it=(char)stream.read();
while(Character.isDigit(it)){
aux[1]+=it;
it=(char)stream.read();
}
名称。添加(aux);
stream.read();
}
}捕获(IOE异常){
系统输出打印(“IOException(read):”;
System.err.println(e.getMessage());
}
}

a
(或第二个示例中的
aux
引用的数组)添加到列表中时,变量
a
仍然引用字符串数组。当您将字符串数组元素重新初始化为空字符串时,也会删除列表中的条目,因为它们是相同的数据结构。您对同一数组有多个引用

您需要为循环中的每个过程创建一个新数组,以便列表元素实际上包含单独的数组。移动初始化数组的行

String[] a = new String[2];
进入while循环。这样,数组将被重新分配,这样局部变量就不会指向先前添加到arrayList中的同一数组

下面是一个小测试程序,它再现了问题:

import java.util.*;

public class DupeArr {
    public void testBad() {
        System.out.println("bad, multiple references to same array");
        List<String[]> list = new ArrayList<String[]>();
        String[] arr = {"a", "b"};
        for (int i = 0; i < 2; i++) {
            arr[0] = "" + i;
            arr[1] = "" + (i * 10);
            list.add(arr);        
        }
        System.out.println(list.get(0)[0]);
        System.out.println(list.get(0)[1]);
        System.out.println(list.get(1)[0]);
        System.out.println(list.get(1)[1]);
        System.out.println(list.get(0)[0].equals(list.get(1)[0]));
        System.out.println(list.get(0)[1].equals(list.get(1)[1]));
        // printing true means these lists point to the same array
        System.out.println("same reference=" + (list.get(0) == list.get(1)));        
    }

    public void testGood() {
        System.out.println("good, new array for each list item");
        List<String[]> list = new ArrayList<String[]>();
        for (int i = 0; i < 2; i++) {
            String[] arr = {"a", "b"};
            arr[0] = "" + i;
            arr[1] = "" + (i * 10);
            list.add(arr);        
        }
        System.out.println(list.get(0)[0]);
        System.out.println(list.get(0)[1]);
        System.out.println(list.get(1)[0]);
        System.out.println(list.get(1)[1]);
        System.out.println(list.get(0)[0].equals(list.get(1)[0]));
        System.out.println(list.get(0)[1].equals(list.get(1)[1]));
        // printing false means these lists point to different arrays
        System.out.println("same reference=" + (list.get(0) == list.get(1)));        
    }
    public static void main(String[] args) {
       DupeArr dupeArr = new DupeArr();
       dupeArr.testBad();
       dupeArr.testGood();
    }
}

“我认为这是因为java进入下一步太快了”,这听起来极不可能,除非有多个线程访问数组或列表。请提供一个简短但完整的程序来演示问题。(如果这种情况“经常发生”的话,这应该相当容易。)如果可以的话,也许可以发布所有代码。肯定还有别的事情发生。你的代码甚至无法编译<代码>a[1}=”“;
bad, multiple references to same array
1
10
1
10
true
true
same reference=true
good, new array for each list item
0
0
1
10
false
false
same reference=false