Java之String,math,文件,集合

String字符串类型的使用:

1.将一个字符串(string)转化为一个char类型的数组:
String a=”1234567”;
char[] b=a.toCharArray();

2.Stringbuffer
进行字符的连接用到Stringbuffer s,通过s.append()进行连接

3.Stringbuffer与String进行相互转化
String a=new String(b) 与 Stringbuffer b=new Stringbuffer(a)

4.字符串数组进行排序:
char[] c;
Arrays.sort(c);
5.大小写字母的转化
toUpperCase()
toLowerCaese()
6.其他
将string类型的字符串进行分割用到的方法是.split(“分割标志”)
StringBuffer下的reverse方法之将字符串进行反向输出

java.math.*:

长整数的加减乘除余数操作
import java.math.*;
BigInteger a,b类型
进行加a.add(b) 减a.subtract(b) 乘a.multiply(b) 除a.divide(b) 余数a.remainder(b)

文件(java.io.*)

1.判断路径是否存在:File file =new File(‘路径\文件名’) file.exists();
如果不存在: File p=file.getParentFile(); 判断该路径是否存在 p.exists()
如果不存在进行创建路径:p.mkdirs();
创建文件:file.createNewFile();

使用BufferedWriter是建立在File 与FileWriter的基础上实现的
步骤是:
File f=new File(‘路径\文件名’);
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
Test.java
import java.io.*;
public class Test {
public static void main(String[] args){
String s;
InputStreamReader isr=new InputStreamReader(System.in);
BufferedReader br=new BufferedReader(isr);
try{
File file=new File("d:/demo12/test.txt");
if(!file.exists()){
File parent=file.getParentFile();
if(!parent.exists()){
parent.mkdirs();
}
file.createNewFile();
}
FileWriter fw=new FileWriter(file);
BufferedWriter bw=new BufferedWriter(fw);
s=br.readLine();
while(s!=null){
if(s.equalsIgnoreCase("exit")){
break;
}
bw.write(s);
bw.newLine();
s=br.readLine();
}
br.close();
bw.flush();
bw.close();
}catch(IOException e){
System.out.println("输入输出异常");
}
}
}

集合

1.泛型集合的使用 在java.util.*这个包中
List<类型名> 集合名=new ArrayList<类型名>();
add添加 get获得,得到 remove删除 contains是否存在
ArrayList是方便于查找。

2.
LinkedList是指针类型的集合 他的父级是List 方便增删
LinkedList的方式是增加:addFirst() addLast() 删除:removerFirst() removerLast() 获取:getFirst() getLast()

3.Map
声明的方式Map m= HashMap()
m.put(key,value)进行添加
这里的value也可以是List下面的集合ArrayList LinkedList
如何获取值:m.get(key)

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
Student.java
public class Student {
private String name;
public Student(String name){
this.name=name;
}
public void show(){
System.out.println(this.name);
}
public String toString() {
return this.name;
}
/*set get方法*/
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
}
Test.java
import java.util.*;
public class Test {
public static void main(String[] args){
Map student=new HashMap();
Student s1=new Student("小明");
Student s2=new Student("小红");
Student s3=new Student("小蓝");
Student s4=new Student("小资");
Student s5=new Student("小黑");
ArrayList<Student> slt1=new ArrayList<Student>();
slt1.add(s1);
slt1.add(s3);
slt1.add(s5);
ArrayList<Student> slt2=new ArrayList<Student>();
slt2.add(s2);
slt2.add(s4);
student.put("计算机学院", slt1);
student.put("外国语学院", slt2);
System.out.println("计算机学院:"+student.get("计算机学院").toString());
System.out.println("外国语学院:"+student.get("外国语学院").toString());
}
}

Fork me on GitHub