目录
集合类
1、list接口,继承自Collection,用以描述有序列表,列表元素可以重复
List接口的常用实现类有两个:
ArrayList——顺序存储方式
LinkedList——链式存储方式
2、Set接口,继承自Collection,用以描述不能包含重复元素的无序集合
Set有一个常用的子接口SortedSet(有序集合),后者所描述的集合中的所有元素按某种顺序呈升序排列
Set接口的常用实现类有三个:HashSet、LinkedHashSet和TreeSet,其中TreeSet实现了SortedSet接口。
3、Map接口,Map未继承Collection接口,不允许包含重复的键,因为键用来唯一标识键值对元素
4、List{Arraylist、LinkedList}
Arraylist 基于可变长度数组
List<String> list=new ArrayList<String>();//(顺序)
List<Integer> list1=new LinkedList<Integer>();//(链式);
LinkedList<String> list2=new LinkedList<String>();
5、{HashSet LinkedHashSet TreeSet},set元素不允许重复
HashSet不保证元素的迭代顺序,不允许重复
LinkedHashSet保证元素的迭代顺序,不允许重复
TreeSet实现了SortedSet接口
Set<String> hashSet=new HashSet<String>();
Set<Student> treeSet=new TreeSet<Student>();
6、Map(key,values),key不能重复
Map{HashMap、LinkedHashMap、TreeMap}
HashMap遍历结果是无序的、LinkedHashMap按照加入顺序遍历、TreeMap按照键值升序遍历
HashMap<Integer,String> map=new HashMap<Integer,String>();
输入输出
流的分类:
(1)按方向分为输入(外部->程序)流和输出(程序->外部)流;
(2)按读取单位分为字节流和字符流;
(3)按是否直接与数据源打交道分为节点流和处理流。
(4)按功能分为文件流、缓冲流、打印流、字节字符转换、管道流、基本数据类型读写、对象串行化等。。。
字节流:Inputstream outputstream
字符流:Reader Writer
InputStream 输入字节流
OutputStream 输出字节流
Reader 输入字符流
Writer 输出字符流1、File类与使用
File f1=new File("");
为了获得从键盘输入的字符流
2、
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
char c[]=new char[521];
int count=br.read(c);//输入字符数组
for(int i=0;i<count;i++)
{
System.out.println(c[i]);
}
3、Scanner实现输入
从键盘输入:File f1=new File("d:\\abc\\xyz.txt");
从文本文件输入:Scanner sc=new Scanner(文本文件名);
4、基于字符流读写文件
(1)FileReader和FileWriter
(2)BufferedReader和BufferedWriter
5、基于字节流读写文件
(1)FileInputStream和FileOutputStream
(2)BufferedInputStream和BufferedOutputStream
例题:在c盘下建立一个文本文件a.txt,读取里面的内容。
方法一:用字节流,一个字节一个字节读取。
FileInputStream fis = new FileInputStream("c:/1.txt");
方法二:用字符流,一个字符一个字符读取
FileReader fr = new FileReader("c:/1.txt");
例题一:
1编写代码完成如下目录、文件操作。
(1)在d盘下建立一个目录dir1
File f1=new File("d:/dir1");
f1.mkdir();
(2)在目录dir1下建立文本文件1.txt,并在里面输入内容。
File f2=new File("d:/dir1/1.txt");
f2.createNewFile();
(3)输出1.txt文件的大小及最后修改日期。
System.out.println(f2.length());
SimpleDateFormat sdf=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
System.out.println(sdf.format(new Date(f2.lastModified())));
(4)将1.txt重命名为2.txt。
File f3=new File("d:/dir1/2.txt");
f2.renameTo(f3);
(5)将目录dir1删除。
f3.delete();
f1.delete();
图形用户界面
1、界面设计
public class Test {
public static void main(String[] ar) {
new Fr();
}
}
class Fr extends JFrame {
Fr() {
this.setTitle("FlowLayout布局");
this.setLayout(new FlowLayout());
this.add(new JLabel("标签"));
this.add(new JButton("命令按钮"));
this.add(new JTextField("文本框", 10));
this.add(new JTextArea(8, 30));
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);//设置窗口关闭的默认行为
this.setSize(400, 250);
this.setVisible(true);
}
}
2、事件处理
两个数求和,求差
public class Test
{public static void main(String[] args )
{new F();
}
}
class F extends JFrame implements ActionListener//容器实现监听接口
{JButton b1=new JButton("求和");
JButton b2=new JButton("求差");
JTextField t1=new JTextField(10);
JTextField t2=new JTextField(10);
JTextField t3=new JTextField(10);
F()
{super("求两个整数的和或差");
setLayout(new GridLayout(2,1));
setSize(400,200);
JPanel p1=new JPanel();
p1.add(t1);p1.add(t2);p1.add(t3);
JPanel p2=new JPanel();
p2.add(b1);p2.add(b2);
add(p1);add(p2);
b1.addActionListener(this);//b1注册监听器,容器作为监听器
b2.addActionListener(this);//b2注册监听器,容器作为监听器
this.pack();
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
setVisible(true);
}
public void actionPerformed(ActionEvent e)//监听接口方法用于事件处理
{try
{int a=Integer.parseInt(t1.getText());
int b=Integer.parseInt(t2.getText());
if(e.getActionCommand().equals("求和"))
t3.setText(a+b+"");
else
t3.setText(a-b+"");
}
catch(NumberFormatException e1)
{JOptionPane.showMessageDialog(this,"num1 or num2 has wrong format");
}
}
}