Java语言基础
常见对象
数组排序
- 冒泡排序
相邻元素两两比较,大的往后放,第一次完毕,最大值的元素就会出现在最大索引处;以此循环
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191029223056544.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTY3NzExOQ==,size_16,color_FFFFFF,t_70)
public static void main(String[] args) {
int[] arr={24, 69, 80, 57, 13};
maopao(arr);
System.out.println(Arrays.toString(arr));
}
private static void maopao(int[] arr) {
for (int i = 0; i < arr.length-1; i++) {
for (int j = 0; j < arr.length-1-i; j++) {
if (arr[j]>arr[j+1]){
int t=arr[j];
arr[j]=arr[j+1];
arr[j+1]=t;
}
}
}
}
打印结果:[13,24,57,69,80]
- 选择排序
从0索引开始,依次和后面元素互相比较,小的往前放,第一次完毕,最小值出现在了最小索引处,以此循环。
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191029223535817.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTY3NzExOQ==,size_16,color_FFFFFF,t_70)
public static void main(String[] args) {
int[] arr={24, 69, 80, 57, 13};
choice(arr);
System.out.println(Arrays.toString(arr));
}
private static void choice(int[] arr) {
for (int j = 0; j < arr.length - 1; j++) {
for (int i = 1 + j; i < arr.length; i++) {
if (arr[j] > arr[i]) {
int t = arr[j];
arr[j] = arr[i];
arr[i] = t;
}
}
}
}
- 直接插入排序
算法思路:直接插入排序,是一种简单的排序方法。它的基本操作是将一个记录插入到一个长度为m 的有序表中,使之仍保持有序,从而得到一个新的长度为m+1的有序列表。
假设有一组元素{k1,k2…,kn},排序开始就认为k1是一个有序序列,让k2插入上述表长为1的有序序列,使之成为一个表长为2的有序序列,然后让k3插入上述表长为2的有序序列,使之成为一个表长为3的有序序列,以此类推,最后让kn插入表长为n-1的有序序列,得到一个表长为n的有序序列。
例如:
原始数据:49,38,65,97,76,13,27
[49],38,65,97,76,13,27 从1索引开始插入
[38,49], ,65,97,76,13,27
[38,49,65] 97,76,13,27
[38,49,65,97] 76,13,27
[38,49,65,76,97]13,27
[13,27,38,49,65,76,97],27
[13,27,38,49,65,76,97]
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] arr={10,8,7,6,5,12,0,-1,-12,0};
for (int i = 0; i < arr.length; i++) {
int j=i;
while (j > 0 && arr[j] < arr[j - 1]) {
int t=arr[j];
arr[j]=arr[j-1];
arr[j-1]=t;
j--;
}
}
System.out.println(Arrays.toString(arr));
for (int i = 1; i < arr.length; i++) {
for (int j = i; j >0; j--) {
if (arr[j]<arr[j-1]){
int t=arr[j];
arr[j]=arr[j-1];
arr[j-1]=t;
}
}
}
System.out.println(Arrays.toString(arr));
}
}
- 快速排序算法思想:
a:分治法:比大小,再分区从数组中取出一个数,作为基准数。
b:分区:将比这个数大或等于的数全放到他的右边,小于他的数全放到他的左边。
c:再对左右区间重复第二步,直到各区间只有一个数。 - 挖坑填数
a:将基准数挖出形成第一个坑。
b:由后向前找比他小的数,找到后挖出此数填到前一个坑中。
c:由前向后找比他大或等于的数,找到后也挖出此数填到前一个坑中。
d:再重复执行2,3两步骤。
例如:将数组5391672408进行快速排序操作
元素 | 5 | 3 | 9 | 1 | 6 | 7 | 2 | 4 | 0 | 8 |
---|
第一次挖坑 | 坑位1 | | 坑位3 | | 坑位5 | 坑位7 | 坑位6 | 坑位4 | 坑位2 | |
第一次填充 | 0 | 3 | 4 | 1 | 2 | 5 | 7 | 6 | 9 | 8 |
… | | | | | | | | | | |
… | | | | | | | | | | |
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] arr = {5, 3, 9, 1, 6, 7, 2, 4, 0, 8};
new Demo4().quickSort(arr, 0, arr.length - 1);
System.out.println(Arrays.toString(arr));
}
public void quickSort(int[] arr, int start, int end) {
if (start < end) {
int index = getIndex(arr, start, end);
quickSort(arr, start, index - 1);
quickSort(arr, index + 1, end);
}
}
private int getIndex(int[] arr, int start, int end) {
int i = start;
int j = end;
int x = arr[i];
while (i < j) {
while (i < j && arr[j] >= x) {
j--;
}
if (i < j) {
arr[i] = arr[j];
i++;
}
while (i < j && arr[i] < x) {
i++;
}
if (i < j) {
arr[j] = arr[i];
j--;
}
}
arr[i] = x;
return i;
}
}
- 归并排序
分而治之,归并的思想:
先将一个数组从中间开始拆分成两个数组,然后一直拆分为只有一个数字的数组;再合并成一个有序数组;
![在这里插入图片描述](https://img-blog.csdnimg.cn/20191029225911796.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTY3NzExOQ==,size_16,color_FFFFFF,t_70)
import java.util.Arrays;
public class Test {
public static void main(String[] args) {
int[] arr={8, 4, 5, 7, 6, 3, 1, 2};
chaifen(arr,0,arr.length-1);
System.out.println(Arrays.toString(arr));
}
private static void chaifen(int[] arr, int start, int end) {
int mid=(start+end)/2;
if (start <end){
chaifen(arr,start,mid);
chaifen(arr,mid+1,end);
guiBing(arr,start,mid,end);
}
}
private static void guiBing(int[] arr, int startIndex, int midIndex, int endIndex) {
int[] arr1=new int[endIndex-startIndex+1];
int i=startIndex;
int j=midIndex+1;
int index=0;
while (i<=midIndex&&j<=endIndex){
if (arr[i]<=arr[j]){
arr1[index]=arr[i];
i++;
}else {
arr1[index]=arr[j];
j++;
}
index++;
}
while (i<=midIndex){
arr1[index]=arr[i];
i++;
index++;
}
while (j<=endIndex){
arr1[index]=arr[j];
j++;
index++;
}
for (int y = 0; y < arr1.length; y++) {
arr[y+startIndex]=arr1[y];
}
}
}
- 二分查找
查找数组的元素对应索引,前提是一个有序数组!将数组从中间分为两部分
public static void main(String[] args) {
int[] arr = {10, 20, 30, 40, 50, 60, 70};
int a=getIndex(arr,50);
System.out.println("索引为:"+a);
}
private static int getIndex(int[] arr, int ele) {
int minIndex = 0;
int maxIndex = arr.length - 1;
int centerIndex = (minIndex + maxIndex) / 2;
while (minIndex <= maxIndex) {
if (ele == arr[centerIndex]) {
return centerIndex;
} else if (ele > arr[centerIndex]) {
minIndex = centerIndex + 1;
} else if (ele < arr[centerIndex]) {
maxIndex = centerIndex - 1;
}
centerIndex = (minIndex + maxIndex) / 2;
}return -1;
}
打印结果为:
索引为:4
- Arrays类概述:
针对数组进行操作的工具类。
提供了排序,查找等功能。 - 成员方法:
public static String toString(int[] a):将某个类型转换为[ ]类型的字符串
public static void sort(int[] a)
public static int binarySearch(int[] a,int key)
public static String toString(int[] a) {
if (a == null)
return "null";
int iMax = a.length - 1;
if (iMax == -1)
return "[]";
StringBuilder b = new StringBuilder();
b.append('[');
for (int i = 0; ; i++) {
b.append(a[i]);
if (i == iMax)
return b.append(']').toString();
b.append(", ");
}
}
- 为了对基本数据类型进行更多的操作,更方便的操作,java就针对每一种基本数据类型提供了对应的类类型。
- 常用操作: 常用的操作之一,用于基本数据类型与字符串之间的转换。
- 基本类型和包装类的对应:
基本类型 | 包装类 |
---|
byte | Byte |
short | Short |
int | Integer |
long | Long |
float | Float |
double | Double |
char | Character |
boolean | Boolean |
- Integer类概述:
Integer 类在对象中包装了一个基本类型 int 的值,该类提供了多个方法,能在 int 类型和 String 类型之间互相转换,还提供了处理 int 类型时非常有用的其他一些常量和方法。 - 构造方法:
public Integer(int value)
public Integer(String s)
- 自动装箱:把基本类型转换为包装类类型
自动拆箱:把包装类类型转换为基本类型
JDK5的新特性 - 注意事项:
在使用时,Integer x = null;代码就会出现NullPointerException;建议先判断是否为null,然后再使用。
public static void main(String[] args) {
Integer.valueOf(127);
Integer integer1=127;
Integer integer2=127;
System.out.println(integer1==integer2);
Integer integer3=128;
Integer integer4=128;
System.out.println(integer3==integer4);
}
public static Integer valueOf(int i) {
if (i >= IntegerCache.low && i <= IntegerCache.high)
return IntegerCache.cache[i + (-IntegerCache.low)];
return new Integer(i);
}
//下篇再见…再见