有没有办法有效地增加实例名称,例如for-loop,就像这样?
for (int i=0; i<10; i++) {
City city+i = new City();
}
该代码显然不起作用,但是我可以做些什么来创建名为City0,City1,City2等的10个对象?
答案 0 :(得分:0)
您可以将城市添加到列表或数组中,然后按索引访问它们。 Java中列表方法的示例:
List<City> cities = new ArrayList<>();
for (int i = 0; i < 10; i++) {
cities.add(new City());
}
然后,您可以像city.get(0)
那样访问第一个city.get(1)
第二个等等。
答案 1 :(得分:0)
<bill>
<bill_detail>
<detail type="T1" heading="PS">
<section type="T1" heading="P1" sub_heading="A">
<data_line type="T1">
<text1>Employees</text1>
<text2>1</text2>
<text3>2</text3>
<text4>3</text4>
<text5>4</text5>
</data_line>
<total_line type="T1">
<text1>Total</text1>
<text3>1</text3>
<text4>2</text4>
<text5>3</text5>
</total_line>
</section>
<section type="T1" sub_heading="B">
<data_line type="T1">
<text1>Single</text1>
<text2>1</text2>
<text3>2</text3>
<text4>3</text4>
<text5>4</text5>
</data_line>
<data_line type="T1">
<text1>Family</text1>
<text2>1</text2>
<text3>2</text3>
<text4>3</text4>
<text5>4</text5>
</data_line>
<total_line type="T1">
<text1>Total</text1>
<text3>1</text3>
<text4>2</text4>
<text5>3</text5>
</total_line>
</section>
<section type="T1" sub_heading="C">
<data_line type="T1">
<text1>Employees</text1>
<text2>1</text2>
<text3>2</text3>
<text4>3</text4>
<text5>4</text5>
</data_line>
<total_line type="T1">
<text1>Total</text1>
<text3>1</text3>
<text4>2</text4>
<text5>3</text5>
</total_line>
</section>
<section type="T1" heading="P2" sub_heading="A">
<data_line type="T1">
<text1>Employees</text1>
<text2>1</text2>
<text3>2</text3>
<text4>3</text4>
<text5>4</text5>
</data_line>
<total_line type="T1">
<text1>Total</text1>
<text3>1</text3>
<text4>2</text4>
<text5>3</text5>
</total_line>
</section>
<section type="T1" sub_heading="B">
<data_line type="T1">
<text1>Single</text1>
<text2>1</text2>
<text3>2</text3>
<text4>3</text4>
<text5>4</text5>
</data_line>
<data_line type="T1">
<text1>Family</text1>
<text2>1</text2>
<text3>2</text3>
<text4>3</text4>
<text5>4</text5>
</data_line>
<total_line type="T1">
<text1>Total</text1>
<text3>1</text3>
<text4>2</text4>
<text5>3</text5>
</total_line>
</section>
<section type="T1" sub_heading="C">
<data_line type="T1">
<text1>Employees</text1>
<text2>1</text2>
<text3>2</text3>
<text4>3</text4>
<text5>4</text5>
</data_line>
<total_line type="T1">
<text1>Total</text1>
<text3>1</text3>
<text4>2</text4>
<text5>3</text5>
</total_line>
</section>
</detail>
</bill_detail>
</bill>