1. 程式人生 > 其它 >2021年7月20日

2021年7月20日

講師:王博

Id10.8.159.99

昨日回顧

方法的宣告

無引數無返回值:public static void 方法名(){}

無引數有返回值:public static 資料型別 方法名(){}

有引數無返回值:public static void 方法名(資料型別 形參){}

有引數有返回值:public static 資料型別 方法名(資料型別 形參){}

Break:終止的作用

Continue:跳出當前迴圈,繼續下一次的迴圈

陣列的宣告:int [] arr = new int[8];

Int [] arr1 = {1,2,3,4};

Int [] arr2 = new int[] {4,5,6,7,8};

陣列:

賦值

取值

今日內容

  1. 定義一個方法,找出int陣列中最大值的索引下標
  2. 定義一個方法,找出int陣列中最小值的索引下標
  3. 定義一個方法,找出陣列中指定元素的下標
  4. 在一個數組中,找出所有指定資料的下標位置【*
  5. 替換陣列中資料為0的元素,使用指定的元素進行替換
  6. 刪除指定下標的元素,要求從刪除的位置開始,之後的元素前移一位
  7. 新增指定的元素,到指定的位置,後面的元素整體後移一位
  8. 氣泡排序
  9. 選擇排序
  10. 找出陣列中最大的元素,放到下標為0的位置
  11. 在上一道題的基礎上,不考慮下標為0的元素,找出陣列中最大的元素,放倒下標為1的位置

定義一個方法,找出int陣列中最大值的索引下標

package com.liujinghe.app;

/**


* 封裝方法的寫法
* 返回int型別陣列最大值下標
*/
public class Demo2 {
public static void main(String[] args) {
int[] arr = {2, 4, 5, 665, 4, 665, 66};
//問題:如果陣列有兩個最大值,怎麼把兩個最大值的索引列印
int i = maxIndexArray(arr);
System.out.println("最大值的索引" + i);
}

/**
* 方法的分析
* 方法的名字 maxIndexArray
* 形參:一個int型別的陣列
* 返回值:int型別
*
* @param arr
* @return
*/
public static int
maxIndexArray(int[] arr) {
int maxIndex = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[i] >= arr[maxIndex]) {
maxIndex = i;
}
}
return maxIndex;

}
}

定義一個方法,找出int陣列中最小值的索引下標

package com.liujinghe.app;

/**
* 2.定義一個方法,找出int陣列中最小值的索引下標
*/
public class Demo3 {
public static void main(String[] args) {
int[] arr = {1, 2, 3, 4, 5, 6};
int i = minIndexArray(arr);
System.out.println("最小值的索引為:" + i);
}

/**
* 方法分析
* 方法名:minIndexArray
* 形參:int 型別陣列
* 返回值:int型別
*/
public static int minIndexArray(int[] arr) {
//假設最小值的索引為0
int minIndex = 0;
for (int i = 1; i < arr.length; i++) {
/**
* [1,2,3,4,5,6]
* i=1 1<6 true arr[1]<=arr[0] false i++
* i=2 2<6 true arr[2]<=arr[0] false i++
* i=3 3<6 true arr[3]<=arr[0] false i++
* i=4 4<6 true arr[4]<=arr[0] false i++
* i=5 5<6 true arr[5]<=arr[0] false i++
* i=6 <6 false 迴圈結束
*/
if (arr[i] <= arr[minIndex]) {
minIndex = i;
}
}
return minIndex;
}
}

定義一個方法,找出陣列中指定元素的下標

package com.liujinghe.app;

public class Demo6 {
//定義一個方法,找出陣列中指定元素的下標
public static void main(String[] args) {
int [] arr = {1,2,3,4,5,6};
int i = indexOf(arr, 3);
System.out.println("查詢元素的索引為:"+i);
}
/**
* 方法分析
* 方法的名字:indexOf
* 方法的引數:int型別陣列,int型別資料
* 返回值:int
*/
public static int indexOf(int [] arr,int find){
int index = -1;
for (int i = 0; i < arr.length; i++) {
if(arr[i] == find){
index = i;
//資料找到,結束迴圈
break;
}
}
return index;
}
}

在一個數組中,找出所有指定資料的下標位置【*

{1,2,3,1,2,3,1,2,3}

1 索引 0,3,6

package com.liujinghe.app;

import java.lang.reflect.Array;

public class Demo8 {
public static void main(String[] args) {
int[] arr = {1,2,3,1,2,3,1,2,3};
int[] indexex = new int[arr.length];
int[] allIndexes = findAllIndexes(arr, 2, indexex);
for (int i = 0; i < allIndexes.length; i++) {
System.out.print(allIndexes[i]+",");
}

}
/**
* 方法的分析
* 方法的名字:findAllIndexes
* 引數:
* int型別陣列
* int型別資料
* int型別陣列,一個空的陣列來接收找到的索引值
* 返回值:int型別陣列
*/
public static int[] findAllIndexes(int [] arr,int find,int[] indexes){
//判斷引數的合法性
if(arr == null ){
System.out.println("引數不合法");
return new int[]{-1};
}
//定義一個變數進行計數
int count = 0;
//使用for迴圈進行查詢
for (int i = 0; i < arr.length; i++) {
if(arr[i] == find){
indexes[count++] = i;
}
}
return indexes;
}
}

替換陣列中資料為0的元素,使用指定的元素進行替換

package com.liujinghe.app;

import java.util.Arrays;

public class Demo10 {
public static void main(String[] args) {
int [] arr = {1,2,3,0,0,0,0,0,0,0,0};
boolean replace = replace(arr, 89);
System.out.println(replace);
System.out.println(Arrays.toString(arr));
}
//
//替換陣列中資料為0的元素,使用指定的元素進行替換
//
/**
* 方法分析
* 方法名字:replace
* 形式引數
* 第一個引數:int型別陣列
* 第二個引數:替換的數值int
* 返回值
* 布林型別資料
*/
public static boolean replace(int [] arr,int newNumber){
//1.引數的合法性
if(arr == null){
System.out.println("引數不合法,陣列不能為空");
return false;
}
//2.使用for迴圈進行替換
for (int i = 0; i < arr.length; i++) {
if(arr[i] == 0){
arr[i] = newNumber;
}
}
return true;

}
}

刪除指定下標的元素,要求從刪除的位置開始,之後的元素前移一位

新增指定的元素,到指定的位置,後面的元素整體後移一位

氣泡排序

選擇排序

找出陣列中最大的元素,放到下標為0的位置

在上一道題的基礎上,不考慮下標為0的元素,找出陣列中最大的元素,放倒下標為1的位置