如何在数组的添加方法中添加多个值?

时间:2019-06-18 06:36:51

标签: android arrays arraylist

我正在尝试向数组添加多个值,但是它返回一个无法解析的错误。有什么办法或解决方法吗?

for (Map.Entry<String,String> entry : hash.entrySet()) {
          String key = entry.getKey();
          String value = entry.getValue();

          if(typePHP.equals("TARGET")) {
              message += key + " " + value + "t\n";
           }else {
              message += key + " " + value + "r\n";
           }
          array.add(Integer.parseInt(key),value,typePHP,gamePHP);
          //Heres the error.
    }

这是错误:Cannot resolve method 'add(int, java.lang.String, java.lang.String, java.lang.String)'

  

我需要单个元素一次存储4个值,并具有类似{109,40,TYPE,GAME}的输出;

3 个答案:

答案 0 :(得分:2)

以这种方式使用addAll:

array.addAll(Arrays.asList( key,value,typePHP,gamePHP) );    


更新: 您需要基于此句子I need the single element to store 4 values at once and have an output like {109,40, TYPE, GAME};

的2d数组

因此,请尝试以下操作:

    ArrayList<ArrayList<String>> array2d = new ArrayList<>();
    array2d.add(new ArrayList<String>() {{
        add("109");
        add("40");
        add("TYPE");
        add("GAME");
    }});

对于您来说,应该是这样的:

ArrayList<ArrayList<String>> array2d = new ArrayList<>();

for (Map.Entry<String,String> entry : hash.entrySet()) {
      String key = entry.getKey();
      String value = entry.getValue();

      if(typePHP.equals("TARGET")) {
          message += key + " " + value + "t\n";
       }else {
          message += key + " " + value + "r\n";
       }

      array2d.add(new ArrayList<String>() {{
        add(key);
        add(value);
        add(typePHP);
        add(gamePHP);
    }});
}

答案 1 :(得分:2)

首先,确保您的ArrayList当前存储的对象的构造函数为(String a,String b,String c)。当您调用array.add(new Item(a,b,c)时,Java需要将其与现有方法进行匹配。

public class Item {
public Item(String a, String b, String c) {
        // initialize your object's variables here in the constructor
    }

}

然后,在使用add()创建新对象时,必须使用new。

array.add(new Item(a, b, c));

从Java中查看:Adding new objects to an ArrayList using a constructor

Oracle关于构造函数的参考:https://docs.oracle.com/javase/tutorial/java/javaOO/constructors.html

答案 2 :(得分:0)

您可以创建所选成员的Custom对象

 public class Item {

    private int key;
      private  String value;
      private String typePHP;
      private String gamePHP;


  public Item(int key, String value, String typePHP, String gamePHP) {
    this.key = key;
    this.value = value;
    this.typePHP = typePHP;
    this.gamePHP = gamePHP;
  }

  public int getKey() {
    return key;
  }

  public void setKey(int key) {
    this.key = key;
  }

  public String getValue() {
    return value;
  }

  public void setValue(String value) {
    this.value = value;
  }

  public String getTypePHP() {
    return typePHP;
  }

  public void setTypePHP(String typePHP) {
    this.typePHP = typePHP;
  }

  public String getGamePHP() {
    return gamePHP;
  }

  public void setGamePHP(String gamePHP) {
    this.gamePHP = gamePHP;
  }
}

现在您可以像

一样使用
ArrayList<Item> itemArray = new ArrayList <>();
for (Map.Entry<String,String> entry : hash.entrySet()) {
      String key = entry.getKey();
      String value = entry.getValue();

      if(typePHP.equals("TARGET")) {
          message += key + " " + value + "t\n";
       }else {
          message += key + " " + value + "r\n";
       }
   itemArray.add(new Item(Integer.parseInt(key),value,typePHP,gamePHP));  
}