当前位置: 代码迷 >> Java相关 >> 关于ArrayList的问题
  详细解决方案

关于ArrayList的问题

热度:278   发布时间:2006-05-20 21:08:00.0
关于ArrayList的问题

import java.io.*;
import java.awt.*;
import java.awt.event.*;
import javax.swing.*;
import javax.swing.filechooser.FileFilter;
import java.util.*;
public class BankOutput extends JFrame implements ActionListener
{
JPanel jp1,jp2,jp3,jp4,jp5;
JLabel jl1,jl2,jl3,jl4;
JTextField jtf1,jtf2,jtf3,jtf4;
JButton jb1,jb2,jb3;
JFileChooser jfc;
ObjectOutputStream oos;
ArrayList list;
String word1,word2,word3,word4;
Bank bank[]=new Bank[10];
int count;

public BankOutput()
{
Container c=getContentPane();
c.setLayout(new GridLayout(5,1,5,5));
list=new ArrayList();
count=0;

jp1=new JPanel();jp2=new JPanel();jp3=new JPanel();jp4=new JPanel();jp5=new JPanel();
jl1=new JLabel("Account number:");jl2=new JLabel("Name:");jl3=new JLabel("Age:");jl4=new JLabel("Money:");
jtf1=new JTextField(10);jtf2=new JTextField(10);jtf3=new JTextField(10);jtf4=new JTextField(10);
jb1=new JButton("Save");jb2=new JButton("Enter");jb3=new JButton("View");

jp1.add(jl1);jp1.add(jtf1);
jp2.add(jl2);jp2.add(jtf2);
jp3.add(jl3);jp3.add(jtf3);
jp4.add(jl4);jp4.add(jtf4);
jp5.add(jb1);jp5.add(jb2);jp5.add(jb3);

c.add(jp1);c.add(jp2);c.add(jp3);c.add(jp4);c.add(jp5);

setSize(600,400);
show();

jb1.addActionListener(this);
jb2.addActionListener(this);

addWindowListener(new WindowAdapter()
{
public void windowClosing(WindowEvent e)
{
System.exit(1);
}
});
}
public static void main(String[] args) throws Exception
{
BankOutput bo=new BankOutput();
}
public void actionPerformed(ActionEvent e)
{
String s=e.getActionCommand();
if(s.equals("Save"))
{
jfc=new JFileChooser();
jfc.setFileFilter(new FileFilter()
{
public boolean accept(File filename)
{
return filename.getName().endsWith(".txt");
}
public String getDescription()
{
return "txt";
}
});
jfc.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
int result = jfc.showSaveDialog(this);
File fileName=jfc.getSelectedFile();
setFile(fileName);
}
if(s.equals("Enter"))
{
word1=jtf1.getText();word2=jtf2.getText();word3=jtf3.getText();word4=jtf4.getText();
bank[count]=new Bank(word1,word2,word3,word4);
list.add(bank[count]);
count++;
}
if(s.equals("View"))
{
for(int i=0;i<count;i++)
System.out.println((String)list.get(i));
}
}
public void setFile(File f)
{
if(f!=null)
try
{
oos=new ObjectOutputStream(new FileOutputStream(f));
}
catch (Exception e)
{
e.getMessage();
}
}
}
class Bank
{
String name,age,number,money;
public Bank(String name,String age,String number,String money)
{
this.number=number;
this.name=name;
this.age=age;
this.money=money;
}
public String getNumber()
{
return number;
}
public String getName()
{
return name;
}
public String getAge()
{
return age;
}
public String getMoney()
{
return money;
}
public String toString()
{
return "number:"+getNumber()+" "+"name:"+" "+getName()+" "+"age:"+getAge()+" "+"money:"+getMoney();
}
};

list.add(bank[count]);在这里我把对象添加到了list里面
可是却利用System.out.println((String)list.get(i));打印不出来啊是为什么呢?
是没有加进去吗?

搜索更多相关的解决方案: ArrayList  

----------------解决方案--------------------------------------------------------
看一下你的count是不是0啊?
----------------解决方案--------------------------------------------------------
  相关解决方案