- package com.tg.test;
- import java.util.ArrayList;
- class Data
- {
- int val;
- }
- public class Test
- {
- public static void main(String args[])
- {
- ArrayList<Data> list = new ArrayList<Data>();
- for (int i = 100; i < 103; i++)
- {
- Data data = new Data();//new Data()要放在循环里,不然你只new了一个Data,循环只是在给同一个data.val赋值,List里也只存了相同的三个data
- data.val = i;
- list.add(data);
- }
- for (Data d : list)
- {
- System.out.println(d.val);
- }
- }
- }
复制代码 |