【问题标题】:How to give java enough time to assign a value to a variable? [closed]如何给java足够的时间给变量赋值? [关闭]
【发布时间】:2015-07-02 01:43:26
【问题描述】:

我有一个循环,其中在循环结束时将 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);
}

通常情况下,列表中存储的是一个空的String。我认为这是因为 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());
    }

}

【问题讨论】:

  • “我认为这是因为 java 进入下一步太快了” 这听起来非常不太可能,除非您有多个线程访问数组或列表。请提供一个简短但完整的程序来演示该问题。 (如果它“经常”发生,那应该相当容易)。
  • 如果可以的话,也许可以发布你所有的代码。一定有其他事情发生。
  • 您的代码甚至无法编译。 a[1} = "";

标签: java arrays string loops arraylist


【解决方案1】:

当您将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();
    }
}

这个的输出是

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

【讨论】:

    猜你喜欢
    • 2014-04-30
    • 2021-12-04
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-01-20
    • 2014-09-10
    相关资源
    最近更新 更多