适合大一复习期末考试的→java
更简单的写法
1.List接口
import java.util.*;
public class Example{
Public static void main(String[] args){
List list = new ArrayList();
List.add(“a”);
List.add(“b”);
List.add(“c”);
Object obj = list.get(0);
String s = (String)obj;
System.out.prrintln(s);
Itearator it = list.iterator();
While(it.hasNext()){
Object obj = it.next();
System.out.println();
}
}
}
2.Hashmap集合
import java.util.*;
Public class Example{
Public static void main(String[] args){
Map map = new HashMap();
map.put (“Tom”,”Tom的衣服”);
map.put (“Jim”,”Jim的鞋子”);
Object obj = map.get(“Jim”);
System.out.prrintln(obj);
}
}
3.Set接口
import java.util.*;
Public class Example{
Public static void main(String[] args){
Set<string> set = new HashSet<string>();
Set.add(“abc”);
Set.add(“efg”);
Set.add(“abc”);
for(String s : set){
System.out.prrintln(s);
}
}
}
4.输入字节流读取文件
import java.io.*;
Public class Example{
Public static void main(String[] args) throws Exception {
FileInputstream in = new FileInputstream(“test.txt”);
int b;
While((b = in.read()) != -1){
System.out.prrintln(b);
}
in.close();
}
}
5.输出字节流
import java.io.*;
Public class Example{
Public static void main(String[] args) throws Exception {
FileOutputstream in = new FileOutputstream(“example.txt”);
String str = “传智播客”;
Byte[] b = str.getBytes();
for(int i =0;i<b.length;i++){
Out.write(b[i]);
}
out.close;
}
}
6.文件的拷贝
import java.io.*;
Public class Example{
Public static void main(String[] args) throws Exception {
Inputstream in = new FileInputstream(“source\\example.txt”);
Outputstream in = new FileInputstream(“target\\example.txt”);
int b;
while((b = in.read())!= -1){
out.write(b);
}
in.close();
out.close;
}
}
7.包装类(基本数据类型和串相互转换)
Public class Example{
Public static void main(String[] args) {
String s = “1234”;
int a = Integer.parseInt(s);//字符串转整数
double d = Double. Integer.parseInt(s);//字符串转小数
String x = String.valueOf(a);//整数转字符
System.out,println(a);
}
}
8.Random类(0到100之间的随机数)
import java.util.Random;
Public class Example{
Public static void main(String[] args) {
Random r = new Random();
for(int x = 0;x<100;x++){
System.out,println(r.nextInt(100));
}
}
}
9.Math类(重点)
Public class Example{
Public static void main(String[] args) {
int a = Math.abs(-32);
long b = Math.round(3.14);
int x = 4;
int y = 6;
int max = Math.max(x,y)
System.out.println();
}
}
10.StringBuffer类
Public class Example{
Public static void main(String[] args) {
StringBuffer b = new StringBuffer(“we are”);
b.appenf(“ students”);
b.Inset(6,“ good”);
System.out,println(b);
}
}
11.字符串的拼接(截取和分割)
Public class Example{
Public static void main(String[] args) {
String str1 = “abc”;
string str2 = “def”;
String str3 = str1 +str2
System.out,println(str3);
}
}
12.字符串的判断操作
Public class Example{
Public static void main(String[] args) {
String str1 = “abc”;
string str2 = “abc”;
booleam t;
if(s1==s2)
t=true
else
t=false
System.out,println(t);
13.String类的初始化
Public class Example{
Public static void main(String[] args) {
String str1 =new String();
String str2 = new String (“abc”);
char[] charArray = new char[]{‘O’,’E’’F’};
String str3 = new String(charArray);
System.out,println(“a” +str1 +”b”);
System.out,println(str2);
System.out,println(str3);
}
}
14.数组求和
Public class Example{
Public static void main(String[] args) {
int[] arr = {1,2,3,4,5};
int sum = 0;
for(int i = 0; i<arr.length;i++)
sum+=(arr[sum]);
}
System.out,println(arr[sum]);
}
}
15.一是方法名相同,二是参数个数或参数类型不同
Public class Example{
Public static void main(String[] args) {
int sum1 = add(1,2);
int sum2 = add(1,2,3);
double sum3 = add(1.2,2.3);
System.out,println(“sum1” + sum1);
System.out,println(“sum2” + sum2);
System.out,println(“sum3” + sum3);
}
Public static int add(int x,int y){
Return x+y;
}
Public static int add (int x,int y){
Return x+y+2;
}
Public static double add(double x, double y){
return x+y;
}
}
16.单词分离
Public class Example{
Public static void main(String[] args) {
String s = “we are good students”;
String[] arr = S.split(“”);
for(int i =0; i<arr.length;i++){
System.out,println(“arr[i]”);
}
for(String a:arr){
System.out,println(a);
}
}
}
17.判断两字符串是否相等
Public class Example{
Public static void main(String[] args) {
String s1 = “abc”;
String s2 = mew String(“abc”);
String s = s1 +s2;
int len = s.length();
s = s.trim();
booleam t;
if(s1 == s2)
t =ture;
else
t=false;
}
}
18.遍历目录下的文件
import java.io.*;
Public class Example{
Public static void main(String[] args) throws Exception {
File file = new File(“D:\\ ”);
if(file.isDirectort()){
String[] names = file.list();
for(int i = 0;i<names.length;i++){
System.out,println(names[i]);
}
}