周总结

Set 实现类的选择
HashSet :速度快,不排序。
Tree :速度慢,有序
LinkedHashSet:排序有顺序。
Map
HashMap
TreeMap
LinkedHashMap

//Iterator 迭代器
//List
List<Student> stus = new ArrayList<Student>();

for (int i = 0; i < 30; i++) {
Student stu = new Student("张" + i, 10 + i, 2010 + i,
new Random().nextInt(100));
stus.add(stu);
}

Iterator<Student> iter = stus.iterator();
while(iter.hasNext()){
Student stu = iter.next();
System.out.println(stu);
}


//Set
Set<Student> stus = new HashSet<Student>();

for (int i = 0; i < 30; i++) {
Student stu = new Student("张" + i, 10 + i, 2010 + i,
new Random().nextInt(100));
stus.add(stu);
}
Iterator<Student> iter = stus.iterator();
while(iter.hasNext()){
Student stu = iter.next();
System.out.println(stu);
}

// Map
Map<String, Integer> map = new HashMap<String, Integer>();
map.put("a", 1);
map.put("b", 2);
map.put("c", 3);
map.put("d", 4);

//使用entrySet将map的键值对映射关系转换成Set集合
Set<Map.Entry<String, Integer>> set = map.entrySet();

//调用Set的iterator方法,得到其迭代器
Iterator<Map.Entry<String, Integer>> mapIter = set.iterator();

while(mapIter.hasNext()){
//next 得到的是Entry对象,而不是直接的键和值
Map.Entry<String, Integer> entry = mapIter.next();

String key = entry.getKey();
Integer val = entry.getValue();
System.out.println("key: " + key + " " + "value: " + val);
}

集合的特点:元素类型可以不同、集合长度可变、空间不固定。

Collection接口和Iterator接口

在集合框架中,分为两种API:
1、装载数据的集合类。
2、操作集合的工具类。

集合接口位于Set接口和List接口的最顶层,是Set接口和List接口的父接口。定义了Collection对象共有的一些基本方法,这些方法分为基本操作、批量操作和数组操作。

Iterator接口是一种用于遍历集合的接口。所谓遍历,是指从集合中取出每一个元素的过程。

List接口:一列数据,数据内容可以重复,以元素安插的次序来放置元素,不会重新排列。
Set 接口:一列数据,数据内容不能重复,使用自己内部的一个排列机制放置元素。
Map接口:一列数据对,使用自己内部的一个排列机制放置元素。

List接口实现类的选择
ArrayList:使用最广泛,集合元素增加或删除操作不频繁时使用。最适合查询。
LinkedList:当需要在集合的中间位置,频繁增加或删除元素时使用。
Vector:与ArrayList类似,但Vector是线程安全的,所以性能要低于ArrayList。

效率排序:LinkedLis > ArrayList >Vector

泛型和for each 循环
泛型:由于集合中保存的匀速都是Object类型,当一个元素从集合中取出来后都是Object类型的对象,所以我们必须对其进行强制类型转换。为了解决这种麻烦,JDK1.5中提供了一种新的处理方式泛型。
如:List<String>list=new ArrayList<String>();
for each 循环大大简化了对于集合的遍历操作。

Map接口不是collection接口的集成,Map接口用于维护键/值对。每个条目包括单独的两部分。
key/Value

在Map中不允许出现重复的键。
key和value可以是任何类的实例。

Swing程序的建立步骤

1、建立容器
2、建立组件
3、将组件添加到容器
4、设置布局
5、添加事件

Swing容器
JApplet、JFrame、JPanel、JScrollPane、JDialog


private int width;
private int height;
private Container contentP;
private JLabel usernameLabel;
private JLabel passwordLabel;
private JTextField usernameField;
private JPasswordField passwordField;
private JRadioButton maleBtn;
private JRadioButton femaleBtn;
private JCheckBox hobbyCheckBox1;
private JCheckBox hobbyCheckBox2;
private JCheckBox hobbyCheckBox3;
private JComboBox addressCombBox;
private JTextArea describeTextArea;
private JButton submitBtn;
private JButton cancelBtn;

public SimpleFrame() {
this.width = 1024;
this.height = 600;
this.setTitle("我的第一个GUI窗体");
this.setSize(this.width, this.height);
Toolkit tool = Toolkit.getDefaultToolkit();
double width = tool.getScreenSize().getWidth();
double height = tool.getScreenSize().getHeight();
this.setLocation((int) (width - this.width) / 2,
(int) (height - this.height) / 2);

this.setIconImage(tool.createImage("img/logo.png"));

// this.setLocationRelativeTo(null);//设置窗体居中
this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

this.addContent();

this.setVisible(true);
}

public void addContent() {
this.contentP = this.getContentPane();
this.contentP.setLayout(null);

//文本 和 输入框
this.usernameLabel = new JLabel("用户名");
this.usernameLabel.setBounds(350, 10, 90,35);
this.usernameLabel.setFont(new Font("宋体", Font.PLAIN, 16));
this.contentP.add(this.usernameLabel);//添加文本组件

this.usernameField = new JTextField();
this.usernameField.setBounds(450, 10, 200,35);
this.contentP.add(this.usernameField);

this.passwordLabel = new JLabel("密 码");
this.passwordLabel.setBounds(350, 60, 90,35);
this.passwordLabel.setFont(new Font("宋体", Font.PLAIN, 16));
this.contentP.add(this.passwordLabel);//添加文本组件

//密码框
this.passwordField = new JPasswordField();
this.passwordField.setBounds(450, 60, 200,35);
this.passwordField.setEchoChar('*');
// this.passwordField.setForeground(Color.RED);
this.contentP.add(this.passwordField);


//单选框
JLabel genderTitle = new JLabel("性别");
genderTitle.setBounds(350, 110, 80, 30);
genderTitle.setFont(new Font("宋体", Font.PLAIN, 16));
this.contentP.add(genderTitle);

this.maleBtn = new JRadioButton("男");
this.maleBtn.setBounds(440, 110, 50,35);
this.contentP.add(this.maleBtn);
this.femaleBtn = new JRadioButton("女");
this.femaleBtn.setBounds(500, 110, 50,35);
this.contentP.add(this.femaleBtn);
ButtonGroup bp = new ButtonGroup();
bp.add(this.maleBtn);
bp.add(this.femaleBtn);

//复选框
this.hobbyCheckBox1 = new JCheckBox("运动");
this.hobbyCheckBox2 = new JCheckBox("电影");
this.hobbyCheckBox3 = new JCheckBox("音乐");
this.hobbyCheckBox1.setBounds(350, 160, 80,35);
this.hobbyCheckBox2.setBounds(440, 160, 80,35);
this.hobbyCheckBox3.setBounds(540, 160, 80,35);

this.contentP.add(this.hobbyCheckBox1);
this.contentP.add(this.hobbyCheckBox2);
this.contentP.add(this.hobbyCheckBox3);

//下拉框
this.addressCombBox = new JComboBox(new String[]{"成都", "德阳", "绵阳", "乐山", "资阳"});
this.addressCombBox.addItem("达州");
this.addressCombBox.addItem("自贡");
this.addressCombBox.setSelectedIndex(5);
this.addressCombBox.setBounds(350, 210, 120, 25);
this.contentP.add(this.addressCombBox);

//文本域
this.describeTextArea = new JTextArea();
this.describeTextArea.setBounds(350, 250, 300, 100);
// this.describeTextArea.setColumns(5);
// this.describeTextArea.setRows(3);
this.describeTextArea.setBorder(BorderFactory.createLineBorder(Color.BLACK));
this.contentP.add(this.describeTextArea);

//按钮
this.submitBtn = new JButton("确定");
this.submitBtn.setBounds(400, 400, 80, 30);
this.contentP.add(this.submitBtn);

this.cancelBtn = new JButton("取消");
this.cancelBtn.setBounds(500, 400, 80, 30);
this.contentP.add(this.cancelBtn);

}

原文地址:https://www.cnblogs.com/gujinshu-wangdan/p/5274161.html