Skip to content

Commit c2e38a5

Browse files
author
shy-coder
committed
JavaDaily
0 parents  commit c2e38a5

33 files changed

+1306
-0
lines changed

.gitignore

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
# Compiled class file
2+
*.class
3+
4+
# Log file
5+
*.log
6+
7+
# BlueJ files
8+
*.ctxt
9+
10+
# Mobile Tools for Java (J2ME)
11+
.mtj.tmp/
12+
13+
# Package Files #
14+
*.jar
15+
*.war
16+
*.nar
17+
*.ear
18+
*.zip
19+
*.tar.gz
20+
*.rar
21+
22+
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
23+
hs_err_pid*

JavaDaily.iml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<module org.jetbrains.idea.maven.project.MavenProjectsManager.isMavenModule="true" type="JAVA_MODULE" version="4">
3+
<component name="NewModuleRootManager" LANGUAGE_LEVEL="JDK_1_8">
4+
<output url="file://$MODULE_DIR$/target/classes" />
5+
<output-test url="file://$MODULE_DIR$/target/test-classes" />
6+
<content url="file://$MODULE_DIR$">
7+
<sourceFolder url="file://$MODULE_DIR$/src/main/java" isTestSource="false" />
8+
<sourceFolder url="file://$MODULE_DIR$/src/main/resources" type="java-resource" />
9+
<sourceFolder url="file://$MODULE_DIR$/src/test/java" isTestSource="true" />
10+
<excludeFolder url="file://$MODULE_DIR$/target" />
11+
</content>
12+
<orderEntry type="jdk" jdkName="1.8" jdkType="JavaSDK" />
13+
<orderEntry type="sourceFolder" forTests="false" />
14+
<orderEntry type="library" name="Maven: junit:junit:4.13" level="project" />
15+
<orderEntry type="library" name="Maven: org.hamcrest:hamcrest-core:1.3" level="project" />
16+
<orderEntry type="library" name="Maven: org.projectlombok:lombok:1.18.14" level="project" />
17+
</component>
18+
</module>

pom.xml

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<parent>
6+
<artifactId>code</artifactId>
7+
<groupId>groupId</groupId>
8+
<version>1.0-SNAPSHOT</version>
9+
<relativePath>../code/pom.xml</relativePath>
10+
</parent>
11+
<modelVersion>4.0.0</modelVersion>
12+
13+
<groupId>com.shy</groupId>
14+
<artifactId>JavaDaily</artifactId>
15+
16+
17+
</project>
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package com.shy.day01;
2+
3+
/**
4+
* @ClassName BubbleSort
5+
* @Description TODO
6+
* @Author shy
7+
* @Date 2020/10/20
8+
**/
9+
public class BubbleSort {
10+
public static void bubbleSort1(int[]a,int n){
11+
int i,j;
12+
for ( i = 0; i <n ; i++) {
13+
for ( j = 1; j <n-i ; j++) {
14+
if (a[j-1]>a[j]){
15+
int temp;
16+
temp =a[j-1];
17+
a[j-1]=a[j];
18+
a[j]=temp;
19+
}
20+
}
21+
}
22+
}
23+
public static void main(String[] args) {
24+
int[] arr={1,2,6,3,12,31,13,1};
25+
BubbleSort.bubbleSort1(arr,arr.length);
26+
for (int i:arr){
27+
System.out.print(i+",");
28+
}
29+
}
30+
}
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package com.shy.day02;
2+
3+
/**
4+
* @ClassName StringTest01
5+
* @Description TODO
6+
* @Author shy
7+
* @Date 2020/10/21
8+
**/
9+
public class StringTest01 {
10+
public static void main(String[] args) {
11+
//创建一个"abc"字符串对象,该对象的内存地址,让s1变量保存
12+
//s1是一个引用,s1指向"abc"对象
13+
String s1 = "abc";
14+
//可以让s1重新指向
15+
//但是"def"字符串本身不可变
16+
s1 = "def";
17+
//在字符串常量池中新建一个"hello"字符串对象,该对象不可变
18+
String s2 = "hello";
19+
//从字符串常量池中直接拿来用
20+
String s3 = "hello";
21+
//判断字符串是否相等不能用==
22+
System.out.println(s2 == s3);
23+
String s4 = new String("abc");
24+
System.out.println(System.identityHashCode(s4));
25+
String s5 = new String("abc");
26+
System.out.println(System.identityHashCode(s5));
27+
System.out.println(s4 == s5);
28+
//以下程序在执行结束后,会在字符串常量池中创建三个字符串对象"aaa""bbb""aaabbb"
29+
String s6 = "aaa";
30+
String s7 = "bbb";
31+
String s8 = s6 + s7;
32+
}
33+
}
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
package com.shy.day02;
2+
3+
/**
4+
* @ClassName StringTest01
5+
* @Description TODO
6+
* @Author shy
7+
* @Date 2020/10/21
8+
**/
9+
public class StringTest02 {
10+
public static void main(String[] args) {
11+
String s1 = "abc";
12+
String s2 = new String("abc");
13+
byte[] bytes = {97,98,99,100};
14+
String s3 = new String(bytes);
15+
//String已经重写了Object中的toString方法
16+
System.out.println(s3);
17+
//1代表下标,2代表长度
18+
String s4 = new String(bytes,1,2);
19+
System.out.println(s4);
20+
char[] c1 = {'我','是','中','国','人',};
21+
String s5 = new String(c1);
22+
System.out.println(s5);
23+
String s6 = new String(c1,2,2);
24+
System.out.println(s6);
25+
}
26+
}
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
package com.shy.day03;
2+
3+
import java.util.Scanner;
4+
5+
/**
6+
* @ClassName StringTest
7+
* @Description TODO
8+
* @Author shy
9+
* @Date 2020/10/22
10+
**/
11+
public class StringTest {
12+
public static void main(String[] args) {
13+
//获取键盘输入并赋值给S1字符串
14+
Scanner s1 = new Scanner(System.in);
15+
System.out.println("请给字符串a赋值");
16+
String a = s1.nextLine();
17+
Scanner s2 = new Scanner(System.in);
18+
System.out.println("请给字符串b赋值");
19+
String b = s2.nextLine();
20+
System.out.println("字符串a的内容为:"+ a);
21+
System.out.println("字符串a的内容为:"+ b);
22+
23+
//将两个字符串比较是否相同
24+
System.out.println("两字符串的内容是否相同?"+a.equals(b));
25+
//比较两字符串是否同一个对象
26+
System.out.println(a == b);
27+
//比较两个字符串的内容是否相同
28+
System.out.println(a.equals(b));
29+
//比较两个字符串(忽略大小写)
30+
System.out.println(a.equalsIgnoreCase(b));
31+
//将两个字符串相加合成为一个新字符串
32+
String c = a + b;
33+
System.out.println("字符串a与b合并之后的内容为:"+c);
34+
//查找某一子字符串是否被包含在此字符串之中,如果包含,包含了多少次
35+
Scanner s3 = new Scanner(System.in);
36+
System.out.println("请输入想要查找的字符串:");
37+
String d = s3.nextLine();
38+
//子字符串是否被包含在此字符串之中,包含输出true,否则输出false
39+
boolean con = c.contains(d);
40+
System.out.println("子字符串是否被包含在此字符串之中:"+con);
41+
//调用count,输出包含次数
42+
System.out.println("包含次数为:"+repetitions(c,d));
43+
//把此子字符串替换为其他的新的指定字符串
44+
Scanner s4 = new Scanner(System.in);
45+
System.out.println("请选择是否替换字符(串):确定(1)/拒绝(2)");
46+
int e = s4.nextInt();
47+
switch (e){
48+
case 1:
49+
Scanner s5 = new Scanner(System.in);
50+
System.out.println("请输入想逃替换的的原字符");
51+
String f = s5.nextLine();
52+
Scanner s6 = new Scanner(System.in);
53+
System.out.println("请输入想逃替换的的原字符");
54+
String g = s6.nextLine();
55+
//将f替换为g
56+
String z = c.replace(f,g);
57+
System.out.println("替换前:"+c);
58+
System.out.println("替换后:"+z);
59+
break;
60+
case 2:
61+
System.out.println("最终的字符串内容为:\n"+c);
62+
break;
63+
default:
64+
System.out.println("该字符(串)有误");
65+
break;
66+
}
67+
}
68+
69+
public static int repetitions(String c,String d){
70+
//创建repetitions统计出现次数
71+
//创建一个计数器,统计次数
72+
int count = 0;
73+
//创建计数器,设开始的地址
74+
int fromIndex = 0;
75+
while ((fromIndex = c.indexOf(d,fromIndex)) != -1){
76+
//子串第一次出现的位置+长度=下一次的起始位置
77+
fromIndex = fromIndex + d.length();
78+
count++;
79+
}
80+
return count;
81+
}
82+
}
Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
package com.shy.day04;
2+
3+
import java.util.Arrays;
4+
5+
/**
6+
* @ClassName ArraysDemo
7+
* @Description Arrays练习
8+
* @Author shy
9+
* @Date 2020/10/23
10+
* java.util.Arrays是一个与数组相关的工具类,里面提供了大量静态方法,用来实现数组常见的操作
11+
* public static String toString(数组):将参数数组变成字符串(按照默认格式:[元素1,元素2,元素3...])
12+
* public static String sort(数组):按照默认升序(从小到大)对数组的元素进行排序
13+
*备注:
14+
* 1.如果是数值,sort默认按照升序从小到大
15+
* 2.如果是字符串,sort默认按照字母排序
16+
* 3.如果是自定义的类型,那么这个自定义的类型需要有Comparable或者Comparator接口的支持
17+
**/
18+
public class ArraysDemo {
19+
public static void main(String[] args) {
20+
int[] intArray = {10,20,30};
21+
//将int[]数组按照默认格式变成字符串
22+
String intStr = Arrays.toString(intArray);
23+
System.out.println(intStr);
24+
25+
//对整型数组排序
26+
int[] array1 = {2,1,3,7,9};
27+
Arrays.sort(array1);
28+
System.out.println(Arrays.toString(array1));
29+
30+
//对字符串数组排序
31+
String[] array2 = {"aaa","ccc","bbb"};
32+
Arrays.sort(array2);
33+
System.out.println(Arrays.toString(array2));
34+
35+
//将一个随机字符串中的所有字符升序排列,并倒序打印
36+
String str = "adsad3123fa";
37+
//如何进行升序排列:sort
38+
//必须是一个数组,才能用Arrays.sort方法
39+
//String-->数组,用toCharArray
40+
char[] chars = str.toCharArray();
41+
//对字符数组进行升序排序
42+
Arrays.sort(chars);
43+
//倒叙遍历
44+
for (int i = chars.length-1; i >= 0; i--){
45+
System.out.print(chars[i] + " ");
46+
}
47+
}
48+
}
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
package com.shy.day04;
2+
3+
/**
4+
* @ClassName MathDemo
5+
* @Description Math练习
6+
* @Author shy
7+
* @Date 2020/10/23
8+
* java.util.Math类是数学县官的工具类,里面提供了大量的静态方法,完成与数学运算相关的操作
9+
* public static double abs(double num):获取绝对值,有多种重载
10+
* public static double ceil(double num):向上取整
11+
* public static double floor(double num):向下取整
12+
* public static double round(double num):四舍五入
13+
* Math.PI代表近似的圆周率常量(double)
14+
**/
15+
public class MathDemo {
16+
public static void main(String[] args) {
17+
//获取绝对值
18+
System.out.println(Math.abs(3.14));
19+
System.out.println(Math.abs(0));
20+
System.out.println(Math.abs(-2.5));
21+
System.out.println("-----------------------");
22+
23+
//向上取整
24+
System.out.println(Math.ceil(3.9));
25+
System.out.println(Math.ceil(3.1));
26+
System.out.println(Math.ceil(3.0));
27+
System.out.println("-----------------------");
28+
29+
//向下取整,抹零
30+
System.out.println(Math.floor(30.1));
31+
System.out.println(Math.floor(30.9));
32+
System.out.println(Math.floor(31.0));
33+
System.out.println("-----------------------");
34+
35+
//四舍五入
36+
System.out.println(Math.round(20.4));
37+
System.out.println(Math.round(30.5));
38+
39+
//计算在-10.8到5.9之间,绝对值大于6或者小于2.1的整数有多少个?
40+
int count = 0;
41+
double min = -10.8;
42+
double max = 5.9;
43+
//这样处理,变量i就是区间之内所有的整数
44+
for (int i = (int) min; i < max; i++){
45+
int abs = Math.abs(i);
46+
if (abs > 6 || abs < 2.1){
47+
System.out.print(i + " ");
48+
count++;
49+
}
50+
}
51+
System.out.println("总共有:"+ count + "个");
52+
}
53+
}
Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
package com.shy.day04;
2+
3+
/**
4+
* @ClassName MyClass
5+
* @Description 成员变量、静态变量、成员方法、静态方法
6+
* @Author shy
7+
* @Date 2020/10/23
8+
**/
9+
public class MyClass {
10+
//成员变量
11+
int num;
12+
//静态变量
13+
static int numStatic;
14+
15+
/**
16+
* 成员方法
17+
*/
18+
public void method() {
19+
System.out.println("这是一个成员方法");
20+
//成员方法可以访问成员变量
21+
System.out.println(num);
22+
//成员方法可以访问静态变量
23+
System.out.println(numStatic);
24+
}
25+
26+
/**
27+
* 静态方法
28+
*/
29+
public static void methodStatic() {
30+
System.out.println("这是一个静态方法");
31+
//静态方法可以访问静态变量
32+
System.out.println(numStatic);
33+
//静态不能直接访问非静态(重点)
34+
//System.out.println(num); 错误写法
35+
//静态方法中不能使用this关键字
36+
//System.out.println(this); X
37+
}
38+
39+
}

0 commit comments

Comments
 (0)