我有一个包含姓名,年龄和汽车的人员列表。我可以打印出一组精确的字符串值。但是,当我向列表中添加另一个名称,年龄,汽车时,仅显示最后的项目/列表。如何打印两组人员数据?
List<People> peopleInfo = getInfo(ppl);
String peopleResults = mapper.writeValueAsString(peopleInfo);
for(People model : peopleInfo) {
People people = new People(model.getName(), model.getAge(), model.getCar());
ToJSON peop = new ToJSON(people);
ObjectMapper mapper2 = new ObjectMapper();
ObjectWriter writer = mapper2.writer(new DefaultPrettyPrinter());
writer.writeValue(new File("file.json"), peop);
getInfo方法
private List<People> getInfo(String ppl) {
List<People> peopleList = new ArrayList<People>();
People people = new People();
//1st Person
people.setName("Tim");
people.setAge("22");
people.setCar("Ford");
//Second Person
people.setName("Susan");
people.setAge("42");
people.setCar("Toyota");
peopleList.add(people);
return peopleList;
}
答案 0 :(得分:3)
您在创建第二个人时重新定义了人,因此只有第二个人被添加到列表中
//1st Person
people.setName("Tim");
people.setAge("22");
people.setCar("Ford");
//Second Person
people.setName("Susan");
people.setAge("42");
people.setCar("Toyota");
peopleList.add(people);
修复方法是创建多个人物对象或在定义后立即添加人物
People people = new People();
People people2 = new People();
People.setName("Time");
People2.setName("Susan");
peopleList.add(people);
peopleList.add(people2);
或后者
people.setName("Tim");
people.setAge("22");
people.setCar("Ford");
peopleList.add(people);
people.setName("Susan");
people.setAge("42");
people.setCar("Toyota");
peopleList.add(people);
答案 1 :(得分:1)
因为people
对象将包含最新值。你需要每次都像这样传递新对象,
People people = new People();
一个简单的例子就是你可以这样做,
People people = new People();
//1st Person
people.setName("Tim");
people.setAge("22");
people.setCar("Ford");
//Second Person
People people2 = new People();
people2.setName("Susan");
people2.setAge("42");
people2.setCar("Toyota");
peopleList.add(people);
peopleList.add(people2);
这样您的peopleList
将会容纳2个对象。
答案 2 :(得分:1)
你真的忘记了一步。 让我们一步一步走:
<plugin>
<artifactId>maven-ear-plugin</artifactId>
<version>2.9.1</version>
<configuration>
<defaultLibBundleDir>lib</defaultLibBundleDir>
<goals>
<goal>generate-application-xml</goal>
<initializeInOrder>true</initializeInOrder>
</goals>
</configuration>
</plugin>
您创建了空列表
List<People> peopleList = new ArrayList<People>();
您创建了一个People people = new People();
//1st Person
people.setName("Tim");
people.setAge("22");
people.setCar("Ford");
对象实例并在其上设置了一些值
People
在这里,您不会创建新人,但会覆盖第一个人的值
//Second Person
people.setName("Susan");
people.setAge("42");
people.setCar("Toyota");
最后,将第一个人(包含第二个人的值)添加到列表中。
您必须为第二个人创建一个新的peopleList.add(people);
个实例,在该个实例上设置值并将其添加到列表中,就像第一个人一样。
答案 3 :(得分:1)
Private Sub Worksheet_Change(ByVal Target As Excel.Range)
If Target.Column = 1 Then
ThisRow = Target.Row
If Target.Value = "Final Recon" Then
With Selection.Validation
.Range ("N" & ThisRow)
.Delete
.Add Type:=xlValidateList, Formula1:="=Final", Formula2:="=Under Review"
End With
Else
Range("N" & ThisRow) = ""
End If
End If
End Sub
您不是在创建另一个人对象并添加上一个对象,而只是修改它。
答案 4 :(得分:0)
你创造了一个&#34; People&#34;对象并设置名称,年龄和汽车。 。 。然后再次设置人物对象的名称,年龄和汽车。您需要两个不同的People对象:
People people1 = new People();
People people2 = new People();
//1st Person
people1.setName("Tim");
people1.setAge("22");
people1.setCar("Ford");
//Second Person
people2.setName("Susan");
people2.setAge("42");
people2.setCar("Toyota");
peopleList.add(people1);
peopleList.add(people2);
另外,您将参数传递给方法getInfo(String ppl)
,但不要使用它。该参数是什么?