Skip to content

Commit 37ea587

Browse files
committed
Java第十二天
1 parent ecaf465 commit 37ea587

File tree

63 files changed

+1069
-0
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

63 files changed

+1069
-0
lines changed
Binary file not shown.

day12/code/day12_Scanner/.classpath

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

day12/code/day12_Scanner/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>day12_Scanner</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
package cn.itcast_01;
2+
3+
import java.io.InputStream;
4+
import java.util.Scanner;
5+
6+
/*
7+
* Scanner:JDK5以后,用于帮助我们实现键盘录入数据的。
8+
*
9+
* 构造方法:
10+
* public Scanner(InputStream source)
11+
*/
12+
public class ScannerDemo {
13+
public static void main(String[] args) {
14+
// 创建键盘录入对象
15+
// Scanner sc = new Scanner(System.in);
16+
17+
// public Scanner(InputStream source)
18+
19+
// System类下有这样的一个成员变量
20+
// public static final InputStream in;
21+
// InputStream is = System.in;
22+
23+
// 假如Demo类下有一个变量
24+
// public static final int x;
25+
// public static final Student s;
26+
// int y = Demo.x;
27+
// Student student = Demo.s;
28+
29+
// InputStream is = System.in; //子类对象
30+
// Scanner sc = new Scanner(is);
31+
32+
Scanner sc = new Scanner(System.in);
33+
}
34+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
package cn.itcast_02;
2+
3+
import java.util.Scanner;
4+
5+
/*
6+
* 成员方法:
7+
* hasNextXxx():判断下一个输入项是不是指定的数据类型
8+
* nextXxx():获取该类型的数据
9+
*
10+
* 注意:
11+
* 键盘录入数据,需要的是int类型,我却给了一个字符串。
12+
* 在控制台就提示:InputMismatchException 输入不匹配异常。
13+
*
14+
*/
15+
public class ScannerDemo {
16+
public static void main(String[] args) {
17+
Scanner sc = new Scanner(System.in);
18+
19+
System.out.println("请输入一个整数:");
20+
21+
if (sc.hasNextInt()) {
22+
int number = sc.nextInt();
23+
System.out.println(number);
24+
}
25+
26+
System.out.println("over");
27+
}
28+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package cn.itcast_03;
2+
3+
import java.util.Scanner;
4+
5+
/*
6+
* public int nextInt():获取一个int类型的数据
7+
* public String nextLine():获取一个字符串类型的数据
8+
*/
9+
public class ScannerDemo {
10+
public static void main(String[] args) {
11+
Scanner sc = new Scanner(System.in);
12+
String s = sc.nextLine();
13+
System.out.println(s);
14+
}
15+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
package cn.itcast_03;
2+
3+
import java.util.Scanner;
4+
5+
/*
6+
* 问题:
7+
* int--int
8+
* String--String
9+
* String--int
10+
*
11+
* int--String有问题,会把回车换行给了String。
12+
*
13+
* 如何解决呢?
14+
* A:重新创建一个新的键盘录入对象。
15+
* B:把键盘所有录入数据全部当作String类型接受。
16+
* 需要什么类型,就把String转换为该类型。
17+
* 至于怎么转,明天讲。
18+
*/
19+
public class ScannerDemo2 {
20+
public static void main(String[] args) {
21+
Scanner sc = new Scanner(System.in);
22+
23+
// int x = sc.nextInt();
24+
// int y = sc.nextInt();
25+
// System.out.println(x);
26+
// System.out.println(y);
27+
28+
// String x = sc.nextLine();
29+
// String y = sc.nextLine();
30+
// System.out.println(x);
31+
// System.out.println(y);
32+
33+
// String x = sc.nextLine();
34+
// int y = sc.nextInt();
35+
// System.out.println(x);
36+
// System.out.println(y);
37+
38+
int x = sc.nextInt();
39+
40+
sc = new Scanner(System.in);
41+
42+
String y = sc.nextLine(); // 把回车换行给了这里
43+
System.out.println(x);
44+
System.out.println(y);
45+
}
46+
}

day12/code/day12_String/.classpath

+6
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src"/>
4+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER/org.eclipse.jdt.internal.debug.ui.launcher.StandardVMType/JavaSE-1.7"/>
5+
<classpathentry kind="output" path="bin"/>
6+
</classpath>

day12/code/day12_String/.project

+17
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<projectDescription>
3+
<name>day12_String</name>
4+
<comment></comment>
5+
<projects>
6+
</projects>
7+
<buildSpec>
8+
<buildCommand>
9+
<name>org.eclipse.jdt.core.javabuilder</name>
10+
<arguments>
11+
</arguments>
12+
</buildCommand>
13+
</buildSpec>
14+
<natures>
15+
<nature>org.eclipse.jdt.core.javanature</nature>
16+
</natures>
17+
</projectDescription>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
eclipse.preferences.version=1
2+
org.eclipse.jdt.core.compiler.codegen.inlineJsrBytecode=enabled
3+
org.eclipse.jdt.core.compiler.codegen.targetPlatform=1.7
4+
org.eclipse.jdt.core.compiler.codegen.unusedLocal=preserve
5+
org.eclipse.jdt.core.compiler.compliance=1.7
6+
org.eclipse.jdt.core.compiler.debug.lineNumber=generate
7+
org.eclipse.jdt.core.compiler.debug.localVariable=generate
8+
org.eclipse.jdt.core.compiler.debug.sourceFile=generate
9+
org.eclipse.jdt.core.compiler.problem.assertIdentifier=error
10+
org.eclipse.jdt.core.compiler.problem.enumIdentifier=error
11+
org.eclipse.jdt.core.compiler.source=1.7
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
String s1 = "admin";
2+
String s2 = "admin";
3+
System.out.println(s1.equals(s2));
4+
5+
private final char value[]; //�ַ����䱾�ʾ���һ���ַ����顣�ײ��Լ�����ʵ��ת����
6+
7+
public boolean equals(Object anObject) {
8+
//this -- s1
9+
//anObject -- s2
10+
11+
//�Ƚ϶���ĵ�ַ�Ƿ���ͬ
12+
if (this == anObject) {
13+
return true;
14+
}
15+
16+
//�ж�anObject�Dz����ַ�����һ������
17+
if (anObject instanceof String) {
18+
//���anObject�������ַ������󣬾�ת��Ϊһ���ַ���
19+
String anotherString = (String) anObject;
20+
21+
int n = value.length;
22+
//value.length --> this.value.length --> s1.toCharArray().length
23+
24+
if (n == anotherString.value.length) {
25+
char v1[] = value; //--> this.value --> s1.toCharArray()
26+
char v2[] = anotherString.value; //--> s2.toCharArray()
27+
int i = 0;
28+
while (n-- != 0) {
29+
if (v1[i] != v2[i])
30+
return false;
31+
i++;
32+
}
33+
return true;
34+
}
35+
}
36+
37+
return false;
38+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package cn.itcast_01;
2+
3+
/*
4+
* 字符串:由多个字符组成的一串数据。
5+
*
6+
* 构造方法:
7+
* public String():创建String对象
8+
* public String(byte[] bytes):把字节数组转成字符串。
9+
* public String(byte[] bytes,int index,int length):把字节数组中的一部分转成字符串
10+
* public String(char[] value):把字符数组转成字符串
11+
* public String(char[] value,int index,int count):把字符数组的一部分转成字符串
12+
* public String(String original):把字符串转成字符串
13+
*
14+
* 问题:
15+
* 1:输出语句输出任何对象名称的时候,默认调用的是该对象的toString()方法。
16+
* 而toString()方法默认输出的是包名...类名@哈希值的十六进制。
17+
* 如果,你用输出语句输出一个对象名称的时候,发现不是这个格式,说明了该类重写了toString()方法。
18+
* 2:返回此字符串的长度
19+
* public int length()
20+
*
21+
* 面试题:
22+
* 数组有length()吗?String有length()吗?
23+
* 没有。
24+
* 有。
25+
*/
26+
public class StringDemo {
27+
public static void main(String[] args) {
28+
// public String():创建String对象
29+
String s1 = new String();
30+
System.out.println("s1:" + s1);
31+
System.out.println("s1.length():" + s1.length());
32+
System.out.println("--------------------------");
33+
34+
// public String(byte[] bytes):把字节数组转成字符串。
35+
byte[] bytes = { 97, 98, 99, 100, 101 };
36+
String s2 = new String(bytes); // 把数值转成对应的字符值
37+
System.out.println("s2:" + s2);
38+
System.out.println("s2.length():" + s2.length());
39+
System.out.println("--------------------------");
40+
41+
// public String(byte[] bytes,int index,int length):把字节数组中的一部分转成字符串
42+
// String s3 = new String(bytes, 1, 2);
43+
String s3 = new String(bytes, 0, bytes.length);
44+
System.out.println("s3:" + s3);
45+
System.out.println("s3.length():" + s3.length());
46+
System.out.println("--------------------------");
47+
48+
// public String(char[] value):把字符数组转成字符串
49+
char[] chs = { 'a', 'b', 'c', 'd', 'e', '林', '青', '霞' };
50+
String s4 = new String(chs);
51+
System.out.println("s4:" + s4);
52+
System.out.println("s4.length():" + s4.length());
53+
System.out.println("--------------------------");
54+
55+
// public String(char[] value,int index,int count):把字符数组的一部分转成字符串
56+
// 需求:我要输出的字符串是:de林青
57+
String s5 = new String(chs, 3, 4);
58+
System.out.println("s5:" + s5);
59+
System.out.println("s5.length():" + s5.length());
60+
System.out.println("--------------------------");
61+
62+
// public String(String original):把字符串转成字符串
63+
String s6 = new String("helloworld");
64+
System.out.println("s6:" + s6);
65+
System.out.println("s6.length():" + s6.length());
66+
System.out.println("--------------------------");
67+
68+
// Java 程序中的所有字符串字面值(如 "abc" )都作为此类的实例实现。
69+
String s7 = "helloworld";
70+
System.out.println("s7:" + s7);
71+
System.out.println("s7.length():" + s7.length());
72+
}
73+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package cn.itcast_02;
2+
3+
/*
4+
* 字符串是常量,它的值在创建之后不能更改
5+
* 看内存图解
6+
*/
7+
8+
public class StringDemo {
9+
public static void main(String[] args) {
10+
String s = "hello";
11+
s += "world";
12+
System.out.println(s);
13+
}
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
package cn.itcast_02;
2+
3+
/*
4+
* String s = new String(“hello”)和String s = “hello”;的区别
5+
*
6+
* ==:比较引用类型,比较的是地址值
7+
* equal():默认比较的是地址值。String类重写了equals()方法,该方法的作用是比较字符串的内容是否相同
8+
*/
9+
public class StringDemo2 {
10+
public static void main(String[] args) {
11+
String s1 = new String("hello");
12+
String s2 = "hello";
13+
14+
System.out.println(s1 == s2); // false
15+
System.out.println(s1.equals(s2)); // true
16+
}
17+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package cn.itcast_02;
2+
3+
/*
4+
* ¿´³ÌÐòд½á¹û
5+
*/
6+
public class StringDemo3 {
7+
public static void main(String[] args) {
8+
String s1 = new String("hello");
9+
String s2 = new String("hello");
10+
System.out.println(s1 == s2); // false
11+
System.out.println(s1.equals(s2)); // true
12+
13+
String s3 = new String("hello");
14+
String s4 = "hello";
15+
System.out.println(s3 == s4); // false
16+
System.out.println(s3.equals(s4)); // true
17+
18+
String s5 = "hello";
19+
String s6 = "hello";
20+
System.out.println(s5 == s6);// true
21+
System.out.println(s5.equals(s6));// true
22+
}
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package cn.itcast_02;
2+
3+
/*
4+
* 看程序写结果
5+
* 字符串变量相加:先开空间,再加内容
6+
* 字符串常量相加:先加,再找,没有再开空间
7+
*/
8+
public class StringDemo4 {
9+
public static void main(String[] args) {
10+
String s1 = "hello";
11+
String s2 = "world";
12+
String s3 = "helloworld";
13+
String s4 = s1 + s2;
14+
String s5 = "hello"+"world";
15+
System.out.println(s4);
16+
System.out.println(s5);
17+
// System.out.println(s3 == s1 + s2);// false
18+
// System.out.println(s3.equals(s1 + s2));// true
19+
// System.out.println(s3 == "hello" + "world");// true
20+
}
21+
}

0 commit comments

Comments
 (0)