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

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

问题描述

我有一个循环,其中在循环的最后一个的String [] 添加到的ArrayList (这是在没有类方法中声明),并在循环的开头说的String [] 被清零的内容:

 的String [] A =新的String [2];
而(真){
  一个[0] =;
  一个[1] =;
  -----一些code ----(即增加了一个[0]和[1])  -----一些code ----
  //让拨打我们的ArrayList列表
  list.add(一);
}

让更多的往往不是,什么是存储在名单上的是一个空的字符串。我认为这是因为Java移动到下一个步骤太快,但我不知道肯定,任何帮助吗?
这是我所有的code的:

 静态的ArrayList<的String []>名称=新的ArrayList<的String []>();
公共静态无效读取(BufferedReader类流){
        的String [] = AUX新的String [2];
        烧焦它= 2;    尝试{
        而(它!=(char)的-1){
            辅助[0] =;
            辅助[1] =;
            IT =(char)的stream.read();            而(Character.isLetter(它)){
                辅助[0] + =它;
                IT =(char)的stream.read();
            }            IT =(char)的stream.read();            而(Character.isDigit(它)){
                辅助[1] + =它;
                IT =(char)的stream.read();
            }
            Names.add(辅助);
            stream.read();
        }
    }赶上(IOException异常五){
        System.out.print(IOException的(读):);
        通信System.err.println(e.getMessage());
    }}


解决方案

当您添加由引用的数组A (或辅助在第二个例子中)到您的列表,变量 A 仍然指向字符串数组。当您重新初始化字符串数组元素为空字符串也擦的条目列表中,因为它们是相同的数据结构。你必须在同一个阵列的多个引用。

您需要创建一个新的数组每次通过循环,使列表元素实际上将包含独立的数组。招行初始化数组

 的String [] A =新的String [2];

要while循环中。这样的阵列将得到重新分配,使局部变量不会指向同一个数组你previously添加到ArrayList

下面是能重现问题的小测试程序:

 进口的java.util。*;公共类DupeArr {
    公共无效testBad(){
        的System.out.println(到同一阵列坏了,多次引用);
        清单<的String []>名单=新的ArrayList<的String []>();
        串[] ARR = {一,B};
        对(INT I = 0; I&2;我++){
            改编[0] =+ I;
            改编[1] =+(我* 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]));
        //打印true意味着这些清单指向同一个数组
        的System.out.println(相同的参考=+(list.get(0)== list.get(1)));
    }    公共无效testGood(){
        的System.out.println(好,每个列表项新阵);
        清单<的String []>名单=新的ArrayList<的String []>();
        对(INT I = 0; I&2;我++){
            串[] ARR = {一,B};
            改编[0] =+ I;
            改编[1] =+(我* 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]));
        //打印false意味着这些清单指向不同的阵列
        的System.out.println(相同的参考=+(list.get(0)== list.get(1)));
    }
    公共静态无效的主要(字串[] args){
       DupeArr dupeArr =新DupeArr();
       dupeArr.testBad();
       dupeArr.testGood();
    }
}

这个输出是

 来同一阵列坏了,多次引用
1
10
1
10
真正
真正
相同的参考= TRUE
每个列表项不错的,新数组
0
0
1
10


相同的参考= FALSE

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

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

}

解决方案

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];

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

The output for this is

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天全站免登陆