Skip to content

Commit 643264c

Browse files
committed
Java Basic Study
0 parents  commit 643264c

17 files changed

Lines changed: 505 additions & 0 deletions

.gitignore

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Created by .ignore support plugin (hsz.mobi)
2+
### Example user template template
3+
### Example user template
4+
5+
# IntelliJ project files
6+
.idea
7+
*.iml
8+
out
9+
gen

src/ArrayListDemo.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.ArrayList;
2+
import java.util.Iterator;
3+
import java.util.List;
4+
5+
public class ArrayListDemo {
6+
public static void main(String[] args) {
7+
List<String> list = new ArrayList<>();
8+
list.add("Hello");
9+
list.add("World");
10+
list.add("Haha");
11+
//第一种遍历方法使用foreach遍历List
12+
for (int i = 0; i < list.size(); i++) {
13+
System.out.println(list.get(i));
14+
}
15+
for (String str :
16+
list) {
17+
System.out.println(str);
18+
}
19+
//第二种遍历,把链表变为数组相关的内容进行遍历
20+
String[] strArray = new String[list.size()];
21+
list.toArray(strArray);
22+
for (int i = 0; i < strArray.length; i++) {
23+
System.out.println(strArray[i]);
24+
}
25+
for (String str :
26+
strArray) {
27+
System.out.println(str);
28+
}
29+
//第三种遍历 使用迭代器进行相关遍历
30+
Iterator<String> iterator = list.iterator();
31+
while (iterator.hasNext()) {
32+
System.out.println(iterator.next());
33+
}
34+
}
35+
}

src/BRReadDemo.java

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
import java.io.BufferedReader;
2+
import java.io.IOException;
3+
import java.io.InputStreamReader;
4+
5+
public class BRReadDemo {
6+
public static void main(String[] args) throws IOException {
7+
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
8+
System.out.println("Enter lines of text.");
9+
System.out.println("Enter 'end' to quit.");
10+
String str;
11+
do {
12+
str = br.readLine();
13+
System.out.println(str);
14+
} while (!str.equals("end"));
15+
}
16+
}

src/DateDemo.java

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
import java.text.SimpleDateFormat;
2+
import java.time.Instant;
3+
import java.time.LocalDate;
4+
import java.time.LocalDateTime;
5+
import java.time.LocalTime;
6+
import java.util.Date;
7+
8+
public class DateDemo {
9+
public static void main(String[] args) {
10+
Date date = new Date();
11+
SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
12+
System.out.println(simpleDateFormat.format(date));
13+
14+
Instant instant = Instant.now();
15+
System.out.println(instant);
16+
17+
Instant instant1 = Instant.ofEpochSecond(20);
18+
System.out.println(instant1);
19+
20+
Instant instant2 = Instant.ofEpochSecond(30, 100);
21+
System.out.println(instant2);
22+
23+
Instant instant3 = Instant.ofEpochMilli(1000);
24+
System.out.println(instant3);
25+
26+
LocalDate localDate = LocalDate.now();
27+
System.out.println(localDate);
28+
29+
LocalDate localDate1 = LocalDate.of(2017, 7, 22);
30+
System.out.println(localDate1);
31+
32+
LocalDate localDate2 = LocalDate.ofYearDay(2018, 100);
33+
System.out.println(localDate2);
34+
35+
LocalDate localDate3 = LocalDate.ofEpochDay(10);
36+
System.out.println(localDate3);
37+
38+
LocalDateTime localDateTime = LocalDateTime.now();
39+
System.out.println(localDateTime);
40+
41+
LocalTime localTime = LocalTime.now();
42+
System.out.println(localTime);
43+
44+
System.out.println(date.getTime());
45+
}
46+
}

src/Employee.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class Employee {
2+
String name;
3+
int age;
4+
String designation;
5+
double salary;
6+
public Employee(String name)
7+
{
8+
this.name=name;
9+
}
10+
11+
public void setAge(int age) {
12+
this.age = age;
13+
}
14+
15+
public void setDesignation(String designation) {
16+
this.designation = designation;
17+
}
18+
19+
public void setSalary(double salary) {
20+
this.salary = salary;
21+
}
22+
public void printEmployee()
23+
{
24+
System.out.println("名字"+name);
25+
System.out.println("年龄"+age);
26+
System.out.println("职位"+designation);
27+
System.out.println("薪水"+salary);
28+
}
29+
}

src/ExceptionDemo.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
2+
public class ExceptionDemo {
3+
public static void main(String[] args) {
4+
try {
5+
System.out.println(10 / 0);
6+
} catch (Exception ex) {
7+
ex.printStackTrace();
8+
}
9+
}
10+
}

src/FileStreamTest.java

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
import java.io.*;
2+
3+
public class FileStreamTest {
4+
public static void main(String[] args) throws IOException {
5+
/* int bWrite[] = {11, 21, 3, 666};
6+
OutputStream outputStream=new FileOutputStream("test.txt");
7+
for (int i = 0; i <bWrite.length ; i++) {
8+
outputStream.write(bWrite[i]);
9+
}
10+
outputStream.close();
11+
12+
InputStream is = new FileInputStream("test.txt");
13+
int size = is.available();
14+
15+
for (int i = 0; i < size; i++) {
16+
System.out.print((char) is.read() + " ");
17+
}
18+
is.close();*/
19+
File file=new File("a.txt");
20+
FileOutputStream fop =new FileOutputStream(file);
21+
OutputStreamWriter writer = new OutputStreamWriter(fop, "UTF-8");
22+
// 构建OutputStreamWriter对象,参数可以指定编码,默认为操作系统默认编码,windows上是gbk
23+
24+
writer.append("中文输入");
25+
// 写入到缓冲区
26+
27+
writer.append("\r\n");
28+
// 换行
29+
30+
writer.append("English");
31+
// 刷新缓存冲,写入到文件,如果下面已经没有写入的内容了,直接close也会写入
32+
33+
writer.close();
34+
// 关闭写入流,同时会把缓冲区内容写入文件,所以上面的注释掉
35+
36+
fop.close();
37+
// 关闭输出流,释放系统资源
38+
39+
FileInputStream fip = new FileInputStream(file);
40+
// 构建FileInputStream对象
41+
42+
InputStreamReader reader = new InputStreamReader(fip, "UTF-8");
43+
// 构建InputStreamReader对象,编码与写入相同
44+
45+
StringBuffer sb = new StringBuffer();
46+
while (reader.ready()) {
47+
sb.append((char) reader.read());
48+
// 转成char加到StringBuffer对象中
49+
}
50+
System.out.println(sb.toString());
51+
reader.close();
52+
// 关闭读取流
53+
54+
fip.close();
55+
// 关闭输入流,释放系统资源
56+
}
57+
}

src/GenericMethodDemo.java

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
public class GenericMethodDemo {
2+
public static void main(String[] args) {
3+
String[] strArray = {"11", "22", "33"};
4+
Integer[] intArray = {1, 2, 3};
5+
Character[] charArray = {'H', 'E', 'L', 'L', 'O'};
6+
Double[] doubleArray = {1.1, 2.2, 3.3};
7+
8+
System.out.println("\n字符串数组元素为:");
9+
printArray(strArray); // 传递一个字符串数组
10+
11+
System.out.println("\n整型数组元素为:");
12+
printArray(intArray); // 传递一个整型数组
13+
14+
System.out.println("\n双精度型数组元素为:");
15+
printArray(doubleArray); // 传递一个双精度型数组
16+
17+
System.out.println("\n字符型数组元素为:");
18+
printArray(charArray); // 传递一个字符型数组
19+
20+
}
21+
22+
public static <T> void printArray(T[] inputArray) {
23+
for (T element : inputArray
24+
) {
25+
System.out.printf("%s ", element);
26+
}
27+
System.out.println();
28+
}
29+
}

src/HelloWorld.java

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
public class HelloWorld {
2+
3+
public static void main(String[] args) {
4+
System.out.println("Hello World!");
5+
}
6+
}

src/MapDemo.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
import java.util.HashMap;
2+
import java.util.Iterator;
3+
import java.util.Map;
4+
5+
public class MapDemo {
6+
public static void main(String[] args) {
7+
Map<String, String> map = new HashMap<>();
8+
map.put("1", "Value1");
9+
map.put("2", "Value2");
10+
map.put("3", "Value3");
11+
//第一种:普遍使用,二次取值
12+
System.out.println("通过Map.keySet遍历key和value:");
13+
for (String key :
14+
map.keySet()) {
15+
System.out.println(key);
16+
}
17+
for (String value :
18+
map.values()) {
19+
System.out.println(value);
20+
}
21+
//第二种
22+
System.out.println("通过Map.entrySet使用iterator遍历key和value:");
23+
Iterator<Map.Entry<String, String>> iterator = map.entrySet().iterator();
24+
while (iterator.hasNext()) {
25+
Map.Entry<String, String> entry = iterator.next();
26+
System.out.println("key=" + entry.getKey() + " and value=" + entry.getValue());
27+
}
28+
//第三种:推荐,尤其是容量大时
29+
System.out.println("通过Map.entrySet遍历key和value");
30+
for (Map.Entry<String, String> entry : map.entrySet()
31+
) {
32+
System.out.println("key=" + entry.getKey() + " and value=" + entry.getValue());
33+
}
34+
}
35+
}

0 commit comments

Comments
 (0)