目录
一、异常处理
1.消极处理:throws **Exception
2.积极处理: Source菜单-Surround With try/catch Block可以帮助我们生成异常处理代码。
try {
// 存在潜在异常的代码
} catch (异常类1 异常对象1) {
//处理异常1的代码
。。。
} catch (异常类n 异常对象n) {
// 处理异常n的代码
} finally {
// 异常发生与否都会执行的代码
}
二、常用系统类
1、基本数据类型
public class demo01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
System.out.println(Integer.MAX_VALUE);//返回int型数据的最大值,2147483647
System.out.println(Integer.MIN_VALUE);//-2147483647
/*
* public Integer(int value) //利用整数生成对象
public Integer(String s) //利用字符串生成对象
*/
Integer i1 = new Integer(123);
Integer i2 = new Integer("123");
System.out.println(i1 == i2);//false
System.out.println(i1.equals(i2));//判断两个值是否相等,true
System.out.println(i1.intValue());// //返回对象的整数值,123
System.out.println(i1.doubleValue());//返回对象的浮点数值,123.0
int i3 = Integer.parseInt("12");//字符串转整数
System.out.println(i3);//12
int i5=Integer.parseInt("100",2);//其它进制转十进制,2进制的100转10进制
System.out.println(i5);//4
String s1=Integer.toString(8,2);//十进制转其它进制,十进制下的8转换成2进制
System.out.println(s1);//1000
}
}
2、字符串类
(1)字符串常量(String)和字符串变量(StringBuffer)
(2)成员方法public StringBuffer append(String str)//在字符串的尾部追加指定字符串,重载方法有多个。
public StringBuffer insert(int index,String str)//在指定位置插入指定字符串,重载方法有多个。
public void setCharAt(int index, char c)// 将index处的字符设置为c
public StringBuffer reverse() //将字符串翻转
public StringBuffer replace(int start,int end, String str)//用指定字符串替换指定位置的子串
import java.util.Scanner;
public class demo01 {
public static void main(String[] args) {
// TODO Auto-generated method stub
StringBuffer s=new StringBuffer("abcd");
s.append("efg");
System.out.println(s);//abcdefg
s.insert(3, "xyz");
System.out.println(s);//abcxyzdefg
s.setCharAt(2, 'h');// 将index处的字符设置为h
System.out.println(s);//abhxyzdefg
s.replace(1, 5, "hello");//用指定字符串替换指定位置的子串
System.out.println(s);//ahellozdefg
System.out.println(s.reverse());//gfedzolleha
}
}
三、集合类
1、描述
输入n个整数,统计每个数出现的次数.
输入
第一行是一个整数n(1<=n<=1000),接下来n行每行一个整数.
输出
第一行输出总共有多少个不同的整数.
接下来每行输出一个整数及这个整数出现的次数,用空格分隔.
输出的整数的顺序与它们在输入中第一次出现的顺序一致(即在输入中先出现的数,也会在输出中先出现)
import java.util.LinkedHashMap;
import java.util.Scanner;
import java.util.Set;
//10.用LinkedHashMap
public class Main{
static LinkedHashMap<Integer,Integer> m=new LinkedHashMap<Integer,Integer>();
static void f(int key)
{
if(m.containsKey(key))
m.put(key, m.get(key)+1);
else
m.put(key, 1);
}
public static void main(String[] args)
{
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
for(int i=0;i<n;i++)
{
f(cin.nextInt());
}
System.out.println(m.size());
Set<Integer> keyset=m.keySet();//set插入
for(Integer key:keyset)
{
System.out.println(key+" "+m.get(key));
}
}
}
2、描述
N只小白鼠(1 < N < 100),每只鼠头上戴着一顶有颜色的帽子。现在称出每只白鼠的重量,要求按照白鼠重量从小到大的顺序输出它们头上帽子的颜色。帽子的颜色用 “red”,“blue”等字符串来表示。不同的小白鼠可以戴相同颜色的帽子。白鼠的重量用整数表示。
输入
输入第一行为一个整数N,表示小白鼠的数目。
下面有N行,每行是一只白鼠的信息。第一个为正整数,表示白鼠的重量,;第二个为字符串,表示白鼠的帽子颜色,字符串长度不超过10个字符。
注意:白鼠的重量各不相同。
输出
按照白鼠的重量从小到大的顺序输出白鼠的帽子颜色。
import java.util.Scanner;
import java.util.TreeMap;
public class Main{
static TreeMap<Integer,String> map1=new TreeMap<Integer,String>();
public int compare(Integer o1,Integer o2){
return o1.compareTo(o2);
}
public static void main(String[] args)
{
Scanner cin=new Scanner(System.in);
int n=cin.nextInt();
for(int i=1;i<=n;i++)
{
int t=cin.nextInt();
String s=cin.next();
map1.put(t,s);
}
for(String value:map1.values())
System.out.println(value);
}
}
四、输入输出
1、流的分类
(1)按方向分为输入(外部->程序)流和输出(程序->外部)流;
(2)按读取单位分为字节流和字符流;
(3)按是否直接与数据源打交道分为节点流和处理流。
(4)按功能分为文件流、缓冲流、打印流、字节字符转换、管道流、基本数据类型读写、对象串行化等。。。
例题一:
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();
import java.io.File;
import java.io.FileWriter;
import java.sql.Date;
import java.text.SimpleDateFormat;
public class test {
public static void main(String[] args) throws Exception {
// TODO Auto-generated method stub
File f1=new File("D:/dir1");//建目录dri1,对象为f1
File newname=new File("D:/dir1/2.txt");//为下面重命名
f1.mkdir();//建目录
if(f1.exists()){
File f2=new File("D:/dir1/1.txt");//建1.txt,对象为f2
f2.createNewFile();//建立txt
//FileWrite fw=new FileWrite("D:/dir1/1.txt");
FileWriter fw =new FileWriter("D:/dir1/1.txt");
fw.write("hello");
fw.close();
System.out.println("文件大小(字节):" + f2.length());
SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd kk:mm:ss");
String lastModified = sdf.format(new Date(f2.lastModified()));
System.out.println("最后修改时间:" + lastModified);
f2.renameTo(newname);
f2.delete();
newname.delete();
f1.delete();
}
}
}
2、单词计数:统计文件中单词出现次数,单词间以空格,tab或回车间隔。
import java.io.File;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Scanner;
import java.util.TreeMap;
public class Test1 {
public static void main(String[] args) throws Exception{
// TODO Auto-generated method stub
Scanner sc=new Scanner(new File("c:/1.txt"));
Map<String,Integer> map1=new TreeMap<String,Integer>();
while(sc.hasNext()) {
String word=sc.next();
Integer v=map1.get(word);
if(v==null) {
map1.put(word,1);
}
else {
map1.put(word, v+1);
}
}
for(Entry<String,Integer> entry:map1.entrySet()) {
System.out.println(entry.getKey()+":"+entry.getValue());
}
}
}
五、图形用户界面开发
编写图形用户界面程序,输入两个正整数数a,b(a<b)输出a,b之间(包含a,b)所有的素数。如果a,b不是整数,给出“格式错误”的提示。
import java.awt.FlowLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.AbstractButton;
import javax.swing.JButton;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JTextField;
public class demo01 {
public static void main(String[] args) {
new F1();
}
}
class F1 extends JFrame implements ActionListener
{
JTextField t1=new JTextField(10);
JTextField t2=new JTextField(10);
JLabel result=new JLabel();//用于在窗口输出
F1()
{
this.setLayout(new FlowLayout());
this.add(new JLabel("input a:"));
this.add(t1);
this.add(new JLabel("input b:"));
this.add(t2);
JButton ok=new JButton("ok");
this.add(ok);
ok.addActionListener(this);
this.setSize(400,500);
this.setDefaultCloseOperation(EXIT_ON_CLOSE);
//this.setDefaultCloseOperation(JFrame,EXIT_ON_CLOSE);
this.setVisible(true);
}
@Override
public void actionPerformed(ActionEvent e) {
// TODO Auto-generated method stub
int a;
int b;
try {
a = Integer.parseInt(t1.getText());
b = Integer.parseInt(t2.getText());
for(int i=a;i<=b;i++)
{
if(isPrime(i))
{
//System.out.println(i);
result.setText(result.getText()+i+" ");
}
}
} catch (NumberFormatException e1) {
// TODO Auto-generated catch block
e1.printStackTrace();
}
}
public boolean isPrime(int a)
{
for(int i=2;i<a-1;i++)
{
if(a%i==0)
{
return false;
}
}
return true;
}
}