【问题标题】:Input is a String array, Output is an ArrayList输入是一个字符串数组,输出是一个 ArrayList
【发布时间】:2019-12-31 17:25:20
【问题描述】:

我的问题在于,我想将 String[] 插入到 ArrayList 中,这很好。

在尝试从ArrayList 获取我的String[] 时,我只收到ArrayList 作为回报。有什么办法可以找回我的String[]

我似乎找不到可以解决这个问题的方法。我想克制自己不要围绕它进行编程。

public ArrayList<String> getAllAttributes() throws IOException {
  BufferedReader reader = new BufferedReader(new FileReader(vmxConfigFile));
  ArrayList<String> attributes = new ArrayList<String>();

  for(int i = 0 ; i < getAmountOfAttributes() ; i++) {
    String line = reader.readLine();
    String[] attribute = line.split("="); 
    attributes.addAll(Arrays.asList(attribute));
  }

  return attributes;
}

public static void main(String[] args) throws IOException {
  VMXConverter vmx = new VMXConverter("file:///C://Users//trisi//Downloads//vCloud-Availability.vmx_ ");
  ArrayList<String> list = vmx.getAllAttributes();

  for(int i = 0; i < list.size(); i++) {
    // What i want to do: String[] x = list.get(i);
    String x = list.get(i); <--this is currently my only option
  }
}

我希望,当我抓取列表中的元素时,它应该是一个大小为 2 的数组,带有 2 个关键字。

相反,我得到一个列表,其中元素不再按数组排序

【问题讨论】:

  • 首先将您的列表类型设为String[],而不仅仅是String。你会得到编译错误,解决这些。砰,完成。

标签: java arrays arraylist type-conversion


【解决方案1】:

这是因为您没有将字符串数组添加到数组列表中,而是将字符串数组的所有元素添加到数组列表中,所以您只会得到一个大数组列表。

你可以做的不是返回一个字符串的arraylist,而是一个字符串数组的arraylist:

public ArrayList<String[]> getAllAttributes() throws IOException {
  BufferedReader reader = new BufferedReader(new FileReader(vmxConfigFile));
  ArrayList<String[]> attributes = new ArrayList<>();

  for(int i = 0 ; i < getAmountOfAttributes() ; i++) {
    String line = reader.readLine();
    String[] attribute = line.split("="); 
    attributes.add(attribute);
  }

  return attributes;
}

public static void main(String[] args) throws IOException {
  VMXConverter vmx = new VMXConverter("file:///C://Users//trisi//Downloads//vCloud-Availability.vmx_ ");
  ArrayList<String[]> list = vmx.getAllAttributes();

  for(int i = 0; i < list.size(); i++) {
    String[] x = list.get(i); 
  }
}

但是我不认为你想要这个。如果我理解得很好,您有一个包含 key=value 对的文件。我建议将它们加载到地图中,这样使用起来会更容易。

您可以像这样加载到地图:

public Map<String,String> getAllAttributesMap() throws IOException {
  BufferedReader reader = new BufferedReader(new FileReader(vmxConfigFile));
  Map<String,String> attributes = new HashMap<>();

  while( (line = br.readLine()) != null){
     String[] attribute = line.split("="); 
     if(attribute.length>0 && attribute[0]!=null) 
        attributes.put(attribute[0],attribute.length<1?null:attribute[1]); //just to avoid null pointer problems...
  } 

  return attributes;
}

因此您可以轻松地从映射中获取键的值。

Map<String,String> attributes=getAllAttributesMap();
System.out.println("Value of foo: "+attributes.get("foo"));
System.out.println("Value of bar: "+attributes.get("bar"));

【讨论】:

    【解决方案2】:

    你需要 ArrayList 而不是 ArrayList 。

    类似这样的:

    public ArrayList<String[]> getAllAttributes() throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(vmxConfigFile));
        ArrayList<String[]> attributes = new ArrayList<>();
    
        for(int i = 0 ; i < getAmountOfAttributes() ; i++) {
            String line = reader.readLine();
            String[] attribute = line.split("=");
            attributes.add(attribute);
        }
    
        return attributes;
    }
    

    【讨论】:

    • 感谢您的帮助,不知道我在想什么:D
    【解决方案3】:

    目前你有一个字符串列表。当您将 String[] 添加到列表中时,数组的每个元素都将单独添加到列表中。
    如果要保持数组原样,则需要使用 String[] 列表。

    public ArrayList<String[]> getAllAttributes() throws IOException {
        BufferedReader reader = new BufferedReader(new FileReader(vmxConfigFile));
        ArrayList<String[]> attributes = new ArrayList<String>();
    
        for(int i = 0 ; i < getAmountOfAttributes() ; i++) {
            String line = reader.readLine();
            String[] attribute = line.split("="); 
            attributes.add(attribute);
        }
    
        return attributes;
    }
    
    public static void main(String[] args) throws IOException {
        VMXConverter vmx = new VMXConverter("file:///C://Users//trisi//Downloads//vCloud-Availability.vmx_ ");
        ArrayList<String[]> list = vmx.getAllAttributes();
    
        for(int i = 0; i < list.size(); i++) {
           String[] x = list.get(i);
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2021-12-13
      • 2016-04-30
      • 2021-01-25
      • 1970-01-01
      • 2014-01-05
      • 2019-04-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多