如何给java足够的时间为变量赋值? [英] How to give java enough time to assign a value to a variable?

查看:28
本文介绍了如何给java足够的时间为变量赋值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个循环,在循环的末尾,一个 String[] 被添加到一个 ArrayList(在类中声明而不是方法)和在循环开始时说 String[] 已清除其内容:

I have a loop in which at the end of the loop a String[] is added to an ArrayList(which is declared in the class not a method) and at the beginning of the loop said String[] is cleared of its contents:

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 进入下一步的速度太快了,但我不确定,有什么帮助吗?这是我所有的代码:

so more often than not, what is stored on the list is an empty String. I think it is because java moves on to the next step too fast but I don't know for sure, any help please? Here is all of my code:

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

}

推荐答案

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

When you add the array referenced by a (or aux in the second example) to your list, The variable a still refers to the string array. When you re-initialize the string array elements to empty strings you also wipe the entries in the list because they are the same data structure. You have multiple references to the same array.

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

You need to create a new array for each pass through the loop so that the list elements will actually contain separate arrays. Move the line initializing the array

String[] a = new String[2];

到 while 循环内.这样数组就会被重新分配,这样局部变量就不会指向你之前添加到 arrayList 的同一个数组.

to inside the while loop. That way the array will get reallocated so that the local variable won't be pointing to the same array you previously added to the arrayList.

这是一个重现问题的小测试程序:

Here's a small test program that reproduces the problem:

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

这篇关于如何给java足够的时间为变量赋值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆