簡體   English   中英

從變量類型的返回值創建列表

[英]Creating a list from returning values of varius types

我有不同領域的課程。

public class Temporary
{
   private Integer id;
   private String name;
   private String value;

public Integer getId() {
      return id;
   }

   public void setId(Integer id) {
      this.id = id;
   }

public String getName() {
      return name;
   }

   public void setName(String name) {
      this.name = name;
   }

   public String getValue() {
      return value;
   }

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

我正在創建一個測試,並且無法創建列表,我需要創建一個列表,但不確定如何創建。

這是我的測試

@Test
   public void samepleList() throws Exception {

      Temporary temp = new Temporary();
      temp.setId(42);
      temp.setName("a");
      temp.setValue("b");
      temp.setId(36);
      temp.setName("c");
      temp.setValue("d");
      temp.setId(42);
      temp.setName("e");
      temp.setValue("f");

      List<Temporary> sampleList = Lists.newArrayList();
      sampleList.add(temp.getId();
      sampleList.add(temp.getName();
      sampleList.add(temp.getValue();

}

我的錯誤發生在sampleList.add(get.getId),如它所說

類型List中的方法add(Temporary)不適用於參數(Integer)。

我將如何修復它並將其放入列表中

您只能將臨時對象添加到List<Temporary> ,不能將int或Strings或whatnots添加到List<Temporary> 您需要重新閱讀泛型和Java集合。

順便說一句,這沒有任何意義:

  Temporary temp = new Temporary(); 
  temp.setId(42);
  temp.setName("a");
  temp.setValue("b");
  temp.setId(36);
  temp.setName("c");
  temp.setValue("d");
  temp.setId(42);
  temp.setName("e");
  temp.setValue("f");

為什么要創建一個臨時對象,然后將字段設置為僅在以后覆蓋這些字段? 這整個事情聞起來很有趣。

也許相反,您想這樣做:

List<Temporary> tempList = new ArrayList<>(); // create list

Temporary temp = new Temporary();  // create Temporary object
temp.setId(42);
temp.setName("a");
temp.setValue("b");
tempList.add(temp);  // add it to the list

temp = new Temporary();  // create new Temporary object
temp.setId(36);
temp.setName("c");
temp.setValue("d");
tempList.add(temp);  // add it to the list

temp = new Temporary();  // create new Temporary object
temp.setId(42);
temp.setName("e");
temp.setValue("f");
tempList.add(temp);  // add it to the list

您的列表只能包含臨時對象

  List<Temporary> sampleList = Lists.newArrayList();
      sampleList.add(temp);

后來get那個temp 它包含您的所有價值觀。 喜歡

Temporary getttingTemp = sampleList.get(0); // 0 is index
Integet myIdIsBack = getttingTemp.getId(); // Yippe, got it

如果要向列表中添加特定類型而不是Temporary對象,則需要將列表聲明更改為具有該特定類型,例如,如果要添加整數:

List<Integer> sampleList = new ArrayList<Integer>();

您不能聲明列表采用某種特定類型,然后再將其傳遞給其他類型。 除非您將Object指定為類型(這可能是個壞主意)

否則,如果要添加Temporary對象並保持列表聲明不變,則需要:

sampleList.add(temp);

您將sampleList聲明為List<Temporary>因此只允許將Temporary對象添加到sampleList。

 List<Temporary> sampleList = Lists.newArrayList();
 sampleList.add(temp);

您可以如下迭代列表。

   for (Temporary t: sampleList ) {
        System.out.println(t.getId());
        System.out.println(t.getName());
        System.out.println(t.getValue());
   }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM