填充java中的列表列表

时间:2016-12-21 17:35:27

标签: java list

我试图在Java中填写列表List<List<String>>,但是当我打印元素时,什么都没有出现!

我的代码:

List<String> temp = new ArrayList<>();
for (int z = 0; z < c.POSList.get("V").size(); z++) {
    temp.add(c.stemmer(c.POSList.get("V").get(z)).get(0));
    temp.addAll(c.ReturnListOFSynoums(c.stemmer(c.POSList.get("V").get(z)).get(0), ""));
    System.out.println(temp); // there are elements !
    verbsMatrix.add(temp);
    temp.clear();
}

for (int s = 0; s < verbsMatrix.size(); s++) {
    for (int r = 0; r < verbsMatrix.get(s).size(); r++) {
        System.out.print(verbsMatrix.get(s).get(r) + "  ");
    }
}

1 个答案:

答案 0 :(得分:4)

您每次都在清除 import java.awt.*; import javax.swing.*; public class aceil extends JFrame { JPanel p4=new JPanel(),p1=new JPanel(),p2=new JPanel(),p3=new JPanel(),p=new JPanel(); JButton cnct=new JButton(),sncr=new JButton(); JTextField user=new JTextField(20); JPasswordField pass=new JPasswordField(20); JLabel wlcm=new JLabel(" "),j1=new JLabel("user name : "),j2=new JLabel("password : "); public aceil(){ f=new formulaire(); f.setVisible(false); setTitle("page d'aceile"); setLocation(400, 350); cnct.addActionListener(this); sncr.addActionListener(this); cnct.setIcon(new ImageIcon(this.getClass().getResource("icon.png"))); cnct.setBorder(null); cnct.setMargin(null); sncr.setIcon(new ImageIcon(this.getClass().getResource("icon2.png"))); sncr.setBorder(null); sncr.setMargin(null); JLabel background=new JLabel(new ImageIcon("C:\\Users\\marie\\workspace\\POAprojet\\src\\font1.png")); add(background); background.setLayout(new BoxLayout(background,BoxLayout.X_AXIS)); background.add(j1); background.add(user); background.add(j2); background.add(pass); background.add(cnct); background.add(sncr); setDefaultCloseOperation(EXIT_ON_CLOSE); pack(); setVisible(true); } public static void main(String[] args) { new aceil(); } ,这与您刚刚添加到temp的实例不同,您不会重新初始化。

尝试在for中声明verbsMatrix,不要清除它。