array学习代码

package cn.itcast_01;


/*
 * 数组排序之冒泡排序:
 * 相邻元素两两比较,大的往后放,第一次完毕,最大值出现在了最大索引处
 */
public class ArrayDemo {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:");
printArray(arr);


/*
// 第一次比较
// arr.length - 1是为了防止数据越界
// arr.length - 1 - 0是为了减少比较的次数
for (int x = 0; x < arr.length - 1 - 0; x++) {
if (arr[x] > arr[x + 1]) {
int temp = arr[x];
arr[x] = arr[x + 1];
arr[x + 1] = temp;
}
}
System.out.println("第一次比较后:");
printArray(arr);


// 第二次比较
// arr.length - 1是为了防止数据越界
// arr.length - 1 - 1是为了减少比较的次数
for (int x = 0; x < arr.length - 1 - 1; x++) {
if (arr[x] > arr[x + 1]) {
int temp = arr[x];
arr[x] = arr[x + 1];
arr[x + 1] = temp;
}
}
System.out.println("第二次比较后:");
printArray(arr);


// 第三次比较
// arr.length - 1是为了防止数据越界
// arr.length - 1 - 2是为了减少比较的次数
for (int x = 0; x < arr.length - 1 - 2; x++) {
if (arr[x] > arr[x + 1]) {
int temp = arr[x];
arr[x] = arr[x + 1];
arr[x + 1] = temp;
}
}
System.out.println("第三次比较后:");
printArray(arr);


// 第四次比较
// arr.length - 1是为了防止数据越界
// arr.length - 1 - 3是为了减少比较的次数
for (int x = 0; x < arr.length - 1 - 3; x++) {
if (arr[x] > arr[x + 1]) {
int temp = arr[x];
arr[x] = arr[x + 1];
arr[x + 1] = temp;
}
}
System.out.println("第四次比较后:");
printArray(arr);
*/


// 既然听懂了,那么上面的代码就是排序代码
// 而上面的代码重复度太高了,所以用循环改进
// for (int y = 0; y < 4; y++) {
// for (int x = 0; x < arr.length - 1 - y; x++) {
// if (arr[x] > arr[x + 1]) {
// int temp = arr[x];
// arr[x] = arr[x + 1];
// arr[x + 1] = temp;
// }
// }
// }


/*
// 由于我们知道比较的次数是数组长度-1次,所以改进最终版程序
for (int x = 0; x < arr.length - 1; x++) {
for (int y = 0; y < arr.length - 1 - x; y++) {
if (arr[y] > arr[y + 1]) {
int temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
System.out.println("排序后:");
printArray(arr);
*/

//由于我可能有多个数组要排序,所以我要写成方法
bubbleSort(arr);
System.out.println("排序后:");
printArray(arr);
}

//冒泡排序代码
public static void bubbleSort(int[] arr){
for (int x = 0; x < arr.length - 1; x++) {
for (int y = 0; y < arr.length - 1 - x; y++) {
if (arr[y] > arr[y + 1]) {
int temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
}


// 遍历功能
public static void printArray(int[] arr) {
System.out.print("[");
for (int x = 0; x < arr.length; x++) {
if (x == arr.length - 1) {
System.out.print(arr[x]);
} else {
System.out.print(arr[x] + ", ");
}
}
System.out.println("]");
}

}----------------------------------------------------------------------------------------------------

package cn.itcast_02;


/*
 * 数组排序之选择排序:
 * 从0索引开始,依次和后面元素比较,小的往前放,第一次完毕,最小值出现在了最小索引处
 */
public class ArrayDemo {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 24, 69, 80, 57, 13 };
System.out.println("排序前:");
printArray(arr);


/*
// 第一次
int x = 0;
for (int y = x + 1; y < arr.length; y++) {
if (arr[y] < arr[x]) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
System.out.println("第一次比较后:");
printArray(arr);


// 第二次
x = 1;
for (int y = x + 1; y < arr.length; y++) {
if (arr[y] < arr[x]) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
System.out.println("第二次比较后:");
printArray(arr);


// 第三次
x = 2;
for (int y = x + 1; y < arr.length; y++) {
if (arr[y] < arr[x]) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
System.out.println("第三次比较后:");
printArray(arr);


// 第四次
x = 3;
for (int y = x + 1; y < arr.length; y++) {
if (arr[y] < arr[x]) {
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
System.out.println("第四次比较后:");
printArray(arr);
*/

/*
//通过观察发现代码的重复度太高,所以用循环改进
for(int x=0; x<arr.length-1; x++){
for(int y=x+1; y<arr.length; y++){
if(arr[y] <arr[x]){
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
System.out.println("排序后:");
printArray(arr);
*/

//用方法改进
selectSort(arr);
System.out.println("排序后:");
printArray(arr);


}

public static void selectSort(int[] arr){
for(int x=0; x<arr.length-1; x++){
for(int y=x+1; y<arr.length; y++){
if(arr[y] <arr[x]){
int temp = arr[x];
arr[x] = arr[y];
arr[y] = temp;
}
}
}
}


// 遍历功能
public static void printArray(int[] arr) {
System.out.print("[");
for (int x = 0; x < arr.length; x++) {
if (x == arr.length - 1) {
System.out.print(arr[x]);
} else {
System.out.print(arr[x] + ", ");
}
}
System.out.println("]");
}
}----------------------------------------------------------------------------------------------------

package cn.itcast_03;


/*
 * 把字符串中的字符进行排序。
 * 举例:"dacgebf"
 * 结果:"abcdefg"
 * 
 * 分析:
 * A:定义一个字符串
 * B:把字符串转换为字符数组
 * C:把字符数组进行排序
 * D:把排序后的字符数组转成字符串
 * E:输出最后的字符串
 */
public class ArrayTest {
public static void main(String[] args) {
// 定义一个字符串
String s = "dacgebf";


// 把字符串转换为字符数组
char[] chs = s.toCharArray();


// 把字符数组进行排序
bubbleSort(chs);


//把排序后的字符数组转成字符串
String result = String.valueOf(chs);

//输出最后的字符串
System.out.println("result:"+result);
}


// 冒泡排序
public static void bubbleSort(char[] chs) {
for (int x = 0; x < chs.length - 1; x++) {
for (int y = 0; y < chs.length - 1 - x; y++) {
if (chs[y] > chs[y + 1]) {
char temp = chs[y];
chs[y] = chs[y + 1];
chs[y + 1] = temp;
}
}
}
}
}-----------------------------------------------------------------------------------------------------

package cn.itcast_04;


/*
 * 注意:下面这种做法是有问题的。
 * 因为数组本身是无序的,所以这种情况下的查找不能使用二分查找。
 * 所以你先排序了,但是你排序的时候已经改变了我最原始的元素索引。
 */
public class ArrayDemo2 {
public static void main(String[] args) {
// 定义数组
int[] arr = { 24, 69, 80, 57, 13 };


// 先排序
bubbleSort(arr);
// 后查找
int index = getIndex(arr, 80);
System.out.println("index:" + index);
}


// 冒泡排序代码
public static void bubbleSort(int[] arr) {
for (int x = 0; x < arr.length - 1; x++) {
for (int y = 0; y < arr.length - 1 - x; y++) {
if (arr[y] > arr[y + 1]) {
int temp = arr[y];
arr[y] = arr[y + 1];
arr[y + 1] = temp;
}
}
}
}


// 二分查找
public static int getIndex(int[] arr, int value) {
// 定义最大索引,最小索引
int max = arr.length - 1;
int min = 0;


// 计算出中间索引
int mid = (max + min) / 2;


// 拿中间索引的值和要查找的值进行比较
while (arr[mid] != value) {
if (arr[mid] > value) {
max = mid - 1;
} else if (arr[mid] < value) {
min = mid + 1;
}


// 加入判断
if (min > max) {
return -1;
}


mid = (max + min) / 2;
}


return mid;
}
}---------------------------------------------------------------------------------------

package cn.itcast_05;


import java.util.Arrays;


/*
 * Arrays:针对数组进行操作的工具类。比如说排序和查找。
 * 1:public static String toString(int[] a) 把数组转成字符串
 * 2:public static void sort(int[] a) 对数组进行排序
 * 3:public static int binarySearch(int[] a,int key) 二分查找
 */
public class ArraysDemo {
public static void main(String[] args) {
// 定义一个数组
int[] arr = { 24, 69, 80, 57, 13 };


// public static String toString(int[] a) 把数组转成字符串
System.out.println("排序前:" + Arrays.toString(arr));


// public static void sort(int[] a) 对数组进行排序
Arrays.sort(arr);
System.out.println("排序后:" + Arrays.toString(arr));


// [13, 24, 57, 69, 80]
// public static int binarySearch(int[] a,int key) 二分查找
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 57));
System.out.println("binarySearch:" + Arrays.binarySearch(arr, 577));
}
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值