【问题标题】:Dynamic variable names in Java [closed]Java中的动态变量名[关闭]
【发布时间】:2012-06-02 16:54:43
【问题描述】:

我正在读取一个逗号分隔的文件并将内容拆分为一个数组。我在 while 循环中执行此操作,因此我希望我的数组动态具有不同的名称,因为文件中的最后一行最终会覆盖我的数组内容。下面是我所涉及的代码片段。

  TextView txt1;
  TextView txt2;

  Scanner in = new Scanner(result);
  in.nextLine(); //skip first line

  while(in.hasNextLine()){
    String line = in.nextLine();
    String []dataset = line.split(",");//store values in array

    txt1.setText(dataset[4]);//Should be 5th element of first line
    txt2.setText(dataset[4]);//Should be 5th element of second line
  }

从上面的代码可以看出,我想在文件的第一行将 txt1 的值设置为 val,与 txt2 类似。我读过 HashMaps 或 Maps 或 ArrayList 会对我有所帮助,但我不确定如何实现这一点。

【问题讨论】:

  • 什么是txt1和txt2?你永远不会在你的代码 sn-p 中声明它们。
  • 你会做类似mapVariable.put("txt1", dataset[4]) 的事情,但这个问题有点令人困惑,所以我真的不知道。
  • @duffymo 正如您正确地提到它的 sn-p txt1 和 txt2 以及我的应用程序中的文本标签一样。您可以将它们视为 System.out.println(dataset[4]); // 其值应该来自文件的第一行,类似 txt2
  • 正如路德维希所问,txt1 和 txt2 是什么?它们的数量是否已知(例如 UI 控件)或任意数量?
  • 它们是 UI 控件,它是我的 android 应用中的 textView。

标签: java arraylist hashmap dynamic-variables


【解决方案1】:

如果我理解您想要正确执行的操作,您希望 txt1 等于第 1 行数组中的第 5 个元素,而 txt2 具有第 2 行第 5 个元素的文本(您的评论说 3ed 元素但代码是第 5 个)。

这可以通过条件和计数器来实现。

Scanner in = new Scanner(result);
   in.nextLine(); //skip first line
   int count = 0;
   while(in.hasNextLine()){

      String line = in.nextLine();
      String []dataset = line.split(",");//store values in array
      if (count == 0){
          txt1.setText(dataset[4]);//Should be 3 element of first line
      }else if (count ==1){
          txt2.setText(dataset[4]);//Should be 3 element of second line
      }
    count++;
   }
  }

编辑:

既然我知道你想要一个数组,那么设置就很容易了。如果您事先不知道要处理的数据量,则外部数组应该是可变的。

ArrayList<String[]> dataSet = new ArrayList(10000)//number should be a guess at the amount of data
Scanner in = new Scanner(result);
   in.nextLine(); //skip first line
   while(in.hasNextLine()){

      String line = in.nextLine();
      String []dataset = line.split(",");//store values in array
      dataSet.add(dataset);
   }
   txt1.setText(dataSet.get(0)[4]);
   txt2.setText(dataSet.get(1)[4]);
  }

如果您愿意,您还可以创建这些文本标签并以类似的方式存储它们,并随时设置文本。

【讨论】:

  • +1。我认为这是一个比我更好的答案。我想我误解了这个问题。如果在 hasNextLine() 上执行 while 循环,期望尝试从同一行设置多个值是荒谬的。令人困惑的问题。
  • 是的,应该改变它。最好只调用 in.nextLine();如果您只对第一行和第二行感兴趣,则存储两次并存储数据
  • @Tiksi,抱歉这个令人困惑的问题,但基本上我认为迈克尔明白了这一点。为了更清楚,因为每次循环运行时,我都会从文件中读取一行,因此数据集的内容会被新行的值覆盖。所以我需要找到一种方法,可以为数据集数组赋予不同的名称,例如dataset1、dataset2、dataset3 等,只要我从文件中读取行。我的代码是 txt1.setText(dataset1[4]) 和 txt2.setText(dataset2[4]);但是这种方法在 Java 中是不可能的。我希望它现在清楚了。
  • 谢谢迈克尔,这正是我想要的。
【解决方案2】:

您需要将数据集数组添加到列表中。所以,在你的 while 循环之前:

List datalist = new ArrayList();

然后,之后:

String []dataset = line.split(",");//store values in array

添加:

datalist.add(dataset);

【讨论】:

    【解决方案3】:

    正准备评论迈克尔的帖子,但空间不足!如果您期望输入中的任意行数,并希望在循环之外处理它们,您可以将 Michael 的方法与我的方法结合起来,最终得到:

       Scanner in = new Scanner(result);
       in.nextLine(); //skip first line
    
       List txtList = new ArrayList(); //create list
    
       while(in.hasNextLine()) {
         String line = in.nextLine();
         String[] dataset = line.split(","); //store values in array
    
         Text txt1 = new Text(); //assuming txt1 is an instance of "Text"
         txt1.setText(dataset[4]); //Should be 5th element of first line
    
         txtList.add(txt1); //add 5th element of each line to list
       }
    
       /*
        * txtList now contains instance of Text containing the 5th value from each
        * line of input
        */
    

    【讨论】:

    • 感谢您的帮助,这也是另一种可能的解决方案...
    猜你喜欢
    • 2016-07-10
    • 1970-01-01
    • 2011-08-18
    • 1970-01-01
    • 2020-07-06
    • 1970-01-01
    • 2016-05-04
    • 2021-11-29
    • 2015-11-15
    相关资源
    最近更新 更多