Skip to content

Commit 6ec9a6f

Browse files
committed
Java第十三天
1 parent 37ea587 commit 6ec9a6f

File tree

71 files changed

+1282
-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.

71 files changed

+1282
-0
lines changed

day13/StringBuffer概述.bmp

2.24 MB
Binary file not shown.
2.24 MB
Binary file not shown.
2.24 MB
Binary file not shown.
Lines changed: 6 additions & 0 deletions
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>
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+
<projectDescription>
3+
<name>day13_Array_Arrays</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>
Lines changed: 11 additions & 0 deletions
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.
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
1:public static String toString(int[] a)
2+
3+
public static String toString(int[] a) {
4+
//a -- arr -- { 24, 13, 69, 80, 57 };
5+
6+
if (a == null)
7+
return "null";
8+
9+
int iMax = a.length - 1; //4
10+
11+
if (iMax == -1) //���������û��Ԫ��
12+
return "[]";
13+
14+
StringBuilder b = new StringBuilder();
15+
b.append('['); //[24, 13, 69, 80, 57]
16+
for (int i = 0; ; i++) {
17+
b.append(a[i]);
18+
if (i == iMax)
19+
return b.append(']').toString();
20+
b.append(", ");
21+
}
22+
}
23+
24+
int[] arr = { 24, 13, 69, 80, 57 };
25+
System.out.println(Arrays.toString(arr));
26+
----------------------------------------------------------
27+
28+
2:public static int binarySearch(int[] a,int key)
29+
30+
public static int binarySearch(int[] a, int key) {
31+
//a -- arr -- { 24, 13, 69, 80, 57 }
32+
//key -- 244
33+
return binarySearch0(a, 0, a.length, key);
34+
}
35+
36+
private static int binarySearch0(int[] a, int fromIndex, int toIndex,
37+
int key) {
38+
//a -- { 13, 24, 57��69�� 80}
39+
//fromIndex = 0
40+
//toIndex = 5
41+
//key = 244;
42+
43+
int low = fromIndex; //0
44+
int high = toIndex - 1; //4
45+
46+
while (low <= high) {
47+
int mid = (low + high) >>> 1; //2,3,4
48+
int midVal = a[mid]; //57,69,80
49+
50+
if (midVal < key)
51+
low = mid + 1; //3��4��5
52+
else if (midVal > key)
53+
high = mid - 1;
54+
else
55+
return mid; // key found
56+
}
57+
return -(low + 1); // key not found.
58+
}
59+
60+
int[] arr = { 24, 13, 69, 80, 57 };
61+
System.out.println(Arrays.binarySearch(arr, 244));
Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
package cn.itcast_01;
2+
3+
/*
4+
* 冒泡排序:
5+
*/
6+
public class ArrayDemo {
7+
public static void main(String[] args) {
8+
// 定义数组
9+
int[] arr = { 24, 69, 80, 57, 13 };
10+
11+
/*
12+
* // 第一次比较 // 减1是为了防止越界 for (int x = 0; x < arr.length - 1 - 0; x++) {
13+
* if (arr[x] > arr[x + 1]) { int temp = arr[x]; arr[x] = arr[x + 1];
14+
* arr[x + 1] = temp; } } // 遍历数组 System.out.println("第一次排序后:");
15+
* printArray(arr);
16+
*
17+
* // 第二次比较 // 减1是为了防止越界 for (int x = 0; x < arr.length - 1 - 1; x++) {
18+
* if (arr[x] > arr[x + 1]) { int temp = arr[x]; arr[x] = arr[x + 1];
19+
* arr[x + 1] = temp; } } // 遍历数组 System.out.println("第二次排序后:");
20+
* printArray(arr);
21+
*
22+
* // 第三次比较 // 减1是为了防止越界 for (int x = 0; x < arr.length - 1 - 2; x++) {
23+
* if (arr[x] > arr[x + 1]) { int temp = arr[x]; arr[x] = arr[x + 1];
24+
* arr[x + 1] = temp; } } // 遍历数组 System.out.println("第三次排序后:");
25+
* printArray(arr);
26+
*
27+
* // 第四次比较 // 减1是为了防止越界 for (int x = 0; x < arr.length - 1 - 3; x++) {
28+
* if (arr[x] > arr[x + 1]) { int temp = arr[x]; arr[x] = arr[x + 1];
29+
* arr[x + 1] = temp; } } // 遍历数组 System.out.println("第四次排序后:");
30+
* printArray(arr);
31+
*/
32+
33+
// for (int y = 0; y < arr.length - 1; y++) {
34+
// // ? 0,1,2,3
35+
// for (int x = 0; x < arr.length - 1 - y; x++) {
36+
// if (arr[x] > arr[x + 1]) {
37+
// int temp = arr[x];
38+
// arr[x] = arr[x + 1];
39+
// arr[x + 1] = temp;
40+
// }
41+
// }
42+
// }
43+
44+
// 冒泡排序
45+
bubbleSort(arr);
46+
// 遍历数组
47+
printArray(arr);
48+
}
49+
50+
// 遍历数组
51+
public static void printArray(int[] arr) {
52+
System.out.print("[");
53+
for (int x = 0; x < arr.length; x++) {
54+
if (x == arr.length - 1) {
55+
System.out.print(arr[x]);
56+
} else {
57+
System.out.print(arr[x] + ", ");
58+
}
59+
}
60+
System.out.println("]");
61+
}
62+
63+
// 排序
64+
public static void bubbleSort(int[] arr) {
65+
for (int x = 0; x < arr.length - 1; x++) {
66+
for (int y = 0; y < arr.length - 1 - x; y++) {
67+
if (arr[y] > arr[y + 1]) {
68+
int temp = arr[y];
69+
arr[y] = arr[y + 1];
70+
arr[y + 1] = temp;
71+
}
72+
}
73+
}
74+
}
75+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package cn.itcast_02;
2+
3+
/*
4+
* 选择排序:
5+
*/
6+
public class ArrayDemo {
7+
public static void main(String[] args) {
8+
// 定义数组
9+
int[] arr = { 24, 69, 80, 57, 13 };
10+
11+
/*
12+
* // 第一次 int x = 0; for (int y = x + 1; y < arr.length; y++) { if
13+
* (arr[y] < arr[x]) { int temp = arr[x]; arr[x] = arr[y]; arr[y] =
14+
* temp; } } System.out.println("第一次比较:"); // 遍历数组 printArray(arr);
15+
*
16+
* // 第二次 x = 1; for (int y = x + 1; y < arr.length; y++) { if (arr[y] <
17+
* arr[x]) { int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } }
18+
* System.out.println("第二次比较:"); // 遍历数组 printArray(arr);
19+
*
20+
* // 第三次 x = 2; for (int y = x + 1; y < arr.length; y++) { if (arr[y] <
21+
* arr[x]) { int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } }
22+
* System.out.println("第三次比较:"); // 遍历数组 printArray(arr);
23+
*
24+
* // 第四次 x = 3; for (int y = x + 1; y < arr.length; y++) { if (arr[y] <
25+
* arr[x]) { int temp = arr[x]; arr[x] = arr[y]; arr[y] = temp; } }
26+
* System.out.println("第四次比较:"); // 遍历数组 printArray(arr);
27+
*/
28+
29+
// for (int x = 0; x < arr.length - 1; x++) {
30+
// for (int y = x + 1; y < arr.length; y++) {
31+
// if (arr[y] < arr[x]) {
32+
// int temp = arr[x];
33+
// arr[x] = arr[y];
34+
// arr[y] = temp;
35+
// }
36+
// }
37+
// }
38+
39+
// 选择排序
40+
selectSort(arr);
41+
// 遍历数组
42+
printArray(arr);
43+
}
44+
45+
// 选择排序
46+
public static void selectSort(int[] arr) {
47+
for (int x = 0; x < arr.length - 1; x++) {
48+
for (int y = x + 1; y < arr.length; y++) {
49+
if (arr[y] < arr[x]) {
50+
int temp = arr[x];
51+
arr[x] = arr[y];
52+
arr[y] = temp;
53+
}
54+
}
55+
}
56+
}
57+
58+
// 遍历数组
59+
public static void printArray(int[] arr) {
60+
System.out.print("[");
61+
for (int x = 0; x < arr.length; x++) {
62+
if (x == arr.length - 1) {
63+
System.out.print(arr[x]);
64+
} else {
65+
System.out.print(arr[x] + ", ");
66+
}
67+
}
68+
System.out.println("]");
69+
}
70+
}
Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package cn.itcast_03;
2+
3+
/*
4+
* 查找元素:
5+
* 基本查找 数组元素无序
6+
* 二分查找 数组元素有序(折半查找)
7+
*/
8+
public class ArrayDemo {
9+
public static void main(String[] args) {
10+
// int[] arr = { 24, 69, 80, 57, 13 };
11+
// 需求:我要查找80在这个数组中的索引
12+
13+
// 新的数组
14+
int[] arr = { 13, 24, 57, 69, 80 };
15+
// 需求:我要查找80在这个数组中的索引
16+
int index = getIndex(arr, 57);
17+
System.out.println(index);
18+
19+
index = getIndex(arr, 88);
20+
System.out.println(index);
21+
22+
}
23+
24+
public static int getIndex(int[] arr, int value) {
25+
int minIndex = 0;
26+
int maxIndex = arr.length - 1;
27+
int middleIndex = (minIndex + maxIndex) / 2;
28+
29+
while (value != arr[middleIndex]) {
30+
if (value > arr[middleIndex]) {
31+
minIndex = middleIndex + 1;
32+
} else if (value < arr[middleIndex]) {
33+
maxIndex = middleIndex - 1;
34+
}
35+
36+
// 加一个判断
37+
if (maxIndex < minIndex) {
38+
return -1;
39+
}
40+
41+
// 重写计算中间索引值
42+
middleIndex = (minIndex + maxIndex) / 2;
43+
}
44+
45+
return middleIndex;
46+
}
47+
48+
// 普通查找
49+
// public static int getIndex(int[] arr, int value) {
50+
// int index = -1;
51+
//
52+
// for (int x = 0; x < arr.length; x++) {
53+
// if (arr[x] == value) {
54+
// index = x;
55+
// break;
56+
// }
57+
// }
58+
//
59+
// return index;
60+
// }
61+
}
Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
package cn.itcast_04;
2+
3+
/*
4+
* 把字符串中的字符进行排序。
5+
*举例:”dacgebf”
6+
*结果:”abcdefg”
7+
*/
8+
public class ArrayDemo {
9+
public static void main(String[] args) {
10+
// 定义字符串
11+
String s = "dacgebf";
12+
13+
// 把字符串转成字符数组
14+
char[] chs = s.toCharArray();
15+
16+
// 对字符数组进行排序
17+
bubbleSort(chs);
18+
19+
// 把排序后的字符数组转成字符串
20+
String result = String.valueOf(chs);
21+
22+
System.out.println(result);
23+
}
24+
25+
public static void bubbleSort(char[] chs) {
26+
for (int x = 0; x < chs.length - 1; x++) {
27+
for (int y = 0; y < chs.length - 1 - x; y++) {
28+
if (chs[y] > chs[y + 1]) {
29+
char temp = chs[y];
30+
chs[y] = chs[y + 1];
31+
chs[y + 1] = temp;
32+
}
33+
}
34+
}
35+
}
36+
}
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package cn.itcast_05;
2+
3+
import java.util.Arrays;
4+
5+
/*
6+
* Arrays:针对数组进行操作的工具类。提供了排序,查找等功能。
7+
*
8+
* 成员方法:
9+
* public static String toString(int[] a):把数组转成字符串
10+
* public static void sort(int[] a):排序(快速排序)
11+
* public static int binarySearch(int[] a,int key):二分查找
12+
*
13+
* 注意:
14+
* 如果数组本身是无序的,不能直接使用二分查找。
15+
* 并且,先排序,再二分也是有问题的,因为这样就改变了数组中元素原始的索引位置。
16+
*/
17+
public class ArraysDemo {
18+
public static void main(String[] args) {
19+
int[] arr = { 24, 13, 69, 80, 57 };
20+
21+
// System.out.println(arr);// [I@a0834e7
22+
23+
// public static String toString(int[] a):把数组转成字符串
24+
System.out.println(Arrays.toString(arr)); // [24, 13, 69, 80, 57]
25+
26+
// public static void sort(int[] a):排序(快速排序)
27+
Arrays.sort(arr);
28+
System.out.println(Arrays.toString(arr)); // [13, 24, 57, 69, 80]
29+
30+
// public static int binarySearch(int[] a,int key):二分查找
31+
// [13, 24, 57, 69, 80]
32+
System.out.println(Arrays.binarySearch(arr, 24));
33+
System.out.println(Arrays.binarySearch(arr, 244)); // ??? -6
34+
}
35+
}

0 commit comments

Comments
 (0)