1. 數組練習題【重點】
1.1 找出數組中最大值的下標位置
/*
方法分析:
public static 不要問
返回值類型:
最大值的下標位置,int類型
方法名:
maxIndexOfArray 最大值下標位置
形式參數列表:
需要一個int類型數組
(int[] array)
方法聲明:
public static int maxIndexOfArray(int[] array)
*/
/**
* 找出數組中的最大值所在下標位置
*
* @param array int類型數組
* @return 最大值下標所在位置
*/
public static int maxIndexOfArray(int[] array) {
// 假設數組中最大值的下標位置爲0
int maxIndex = 0;
for (int i = 1; i < array.length; i++) {
// 如果發現maxIndex對應元素小於下標爲i的元素
if (array[maxIndex] < array[i]) {
// 保留i值
maxIndex = i;
}
}
return maxIndex;
}
1.2 找出數組中最小值的下標位置
/*
方法分析:
public static 不要問
返回值類型:
找出對應元素的下標位置,返回值爲int類型
方法名:
minIndexOfArray
形式參數列表:
(int[] array)
方法聲明:
public static int minIndexOfArray(int[] array)
*/
/**
* 找出數組中最小值的下標位置
*
* @param array int類型數組
* @return 返回值是當前數組中最小值的下標位置
*/
public static int minIndexOfArray(int[] array) {
int minIndex = 0;
for (int i = 1; i < array.length; i++) {
if (array[minIndex] > array[i]) {
minIndex = i;
}
}
return minIndex;
}
1.3 找出數組中指定元素的下標位置
/*
方法分析:
public static
返回值類型:
返回值是數組的下標位置,爲int類型
方法名:
indexOf
形式參數列表:
1. int類型數組
2. 告知方法指定查詢的數據
(int[] array, int find);
方法聲明:
public static int indexOf(int[] array, int find)
*/
/**
* 找出指定元素的下標位置
*
* @param array 指定的int類型數組
* @param find 指定查詢的數據,爲int類型
* @return 找到對應下標位置,返回值大於等於0,沒有找到返回-1
*/
public static int indexOf(int[] array, int find) {
// 這裏假設找不到對應的數據
int index = -1;
// 利用循環遍歷數組
for (int i = 0; i < array.length; i++) {
// 發現存在數據和指定find數據一致
if (find == array[i]) {
// 保留下標
index = i;
// 沒有繼續循環下去的必要,終止循環
break;
}
}
return index;
}
1.4 獲取數組中指定下標的元素
/**
* 找出指定下標的元素
*
* @param array 指定的數組
* @param index 指定的下標位置
* @return 對應當前下標的元素,爲int類型
*/
public static int get(int[] array, int index) {
// 參數合法性判斷的思想
if (index > array.length - 1 || index < 0) {
System.out.println("Input Parameter is Invalid!");
System.exit(0);
}
return array[index];
}
1.5. 找出指定元素在指定數組中所有下標位置 【難點】
/*
要求:
a. 不允許在方法內打印展示
b. 考慮多個數據情況
c. 需要在方法外獲取到下標數據信息
d. 不允許使用數組作爲返回值
方法分析:
public static 不要問
返回值類型:
int類型,返回找的指定數據的個數
方法名:
findAll
形式參數列表:
a. 指定查詢數據的數組,int類型數組
b. 指定查詢的目標數據,int類型
c. 一個可以保存下標的int類型數組,數組的容量是源數據數組容量
分析:
所有指定元素的下標位置
1. 數據個數不確定,但是有極值,數據多個。
2. 下標是 int類型
需要
int類型數組,數組容量是指定源數據數組的容量
方法聲明:
public static int findAll(int[] arr, int find, int[] index)
*/
/**
* 在指定數組中找出指定數據,保存指定數據的下標位置到index數組中。
*
* @param arr 源數據int類型數組
* @param find 需要查找的指定數據爲int類型
* @param index 保存找到數據下標位置的數組,要求和源數據數組容量一致
* @return 找到目標數據的個數,沒有找到返回0
*/
public static int findAll(int[] arr, int find, int[] index) {
/*
參數合法性判斷
null == arr 你目前可以理解成給予當前方法的參數中數組爲“空”
arr.index == 0 給予當前方法的數組容量爲0
index.length < arr.length 保存下標的數組容量和源數據數組容量一致
*/
if (null == arr || null == index || arr.length == 0
|| index.length == 0 || index.length < arr.length) {
System.out.println("Input Parameter is Invalid!");
return 0;
}
// int類型變量,計數當前找到的元素個數
// 還有一個功能,是下一次存儲指定數據下標的位置【尾插法】
int size = 0;
// 利用循環遍歷源數據數組
for (int i = 0; i < arr.length; i++) {
// 發現數組中下標爲i的元素和指定find值一致,保存對應下標位置
if (find == arr[i]) {
// 下標保存到index數組中
index[size] = i;
size += 1;
}
}
return size;
}
1.6 在指定位置插入指定元素【難點】
/*
存在一個數組,數組中的元素爲
int[] array = {1, 3, 5, 7, 9, 11, 13, 15, 17, 0};
要求
1. 0是無效元素,僅佔位使用
2. 當前數組中【有效元素】個數爲9
需求
在該數組中的指定下標位置放入指定元素
方法分析:
public static 不要問
返回值類型:
void 沒有返回值,不太合適
int 也不太合適
boolean 判斷添加操作是否成功
方法名:
add 添加
形式參數列表:
1. 插入數據的數組
2. 指定插入數據的下標位置
3. 插入的數據
(int[] arr, int index, int insert)
方法聲明:
public static boolean add(int[] arr, int index, int insert);
*/
/**
* 在數組中指定下標位置插入指定元素,操作成功返回true,否則返回false
*
* @param arr 指定插入數據的int類型數組,並且存在空餘位置
* @param index 指定插入數據的下標位置,不能超出數組的有效下標範圍
* @param insert 指定插入的數據
* @return 操作成功返回true,否則返回false
*/
public static boolean add(int[] arr, int index, int insert) {
// 參數合法性判斷
if (null == arr || arr.length == 0 || index < 0
|| index > arr.length - 1) {
System.out.println("Input Parameter is Invalid!");
// 方法運行失敗,返回false
return false;
}
// 從最後一個有效位置開始,到插入位置循環結束,目的是移動數組中的元素
/*
1 3 5 7 9 11 13 15 17 0
index = 5 insert = 20
arr[9] = arr[8];
arr[8] = arr[7];
arr[7] = arr[6];
arr[6] = arr[5];
*/
for (int i = arr.length - 1; i > index; i--) {
arr[i] = arr[i - 1];
}
arr[index] = insert;
return true;
}
1.7 刪除數組中的指定下標的元素【難點】
/*
存在一個數組,數組中的元素爲
int[] array = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
要求:
1. 0是無效元素,僅佔位使用
需求:
在當前數組中刪除指定下標的元素
例如:
指定下標5
結果 {1, 3, 5, 7, 9, 13, 15, 17, 19, 0}
0佔位!!!
方法分析:
public static 不要問
返回值類型:
boolean
方法名:
remove
形式參數列表:
1. 刪除數據的目標數組
2. 指定的下標位置
(int[] arr, int index)
方法聲明:
public static boolean remove(int[] arr, int index)
*/
/**
* 刪除指定下標的元素
*
* @param arr 指定刪除數據的int類型數組
* @param index 指定刪除的下標位置,不能超出數組的有效下標範圍
* @return 刪除成功返回true,失敗返回false
*/
public static boolean remove(int[] arr, int index) {
// 參數合法性判斷
if (null == arr || arr.length == 0 || index < 0
|| index > arr.length - 1) {
System.out.println("Input Parameter is Invalid!");
// 參數異常告知開發者這裏有問題
return false;
}
/*
1 3 5 7 9 11 13 15 17 19
index = 5
arr[5] = arr[6];
arr[6] = arr[7];
arr[7] = arr[8];
arr[8] = arr[9];
arr[9] = arr[10] ???
*/
for (int i = index; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
arr[arr.length - 1] = 0;
return true;
}
1.8 找出數組中最大值元素,放到下標爲0的位置
main(String[] args) {
int[] arr = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
int maxIndex = 0;
for (int i = 1; i < arr.length; i++) {
// 如果發現maxIndex對應元素小於下標爲i的元素
if (arr[maxIndex] < arr[i]) {
// 保留i值
maxIndex = i;
}
}
// 假如最大值的下標不是0,可以交換
if (maxIndex != 0) {
int temp = arr[maxIndex];
arr[maxIndex] = arr[0];
arr[0] = temp;
}
System.out.println(Arrays.toString(arr))
}
1.9 接上一題,找出數組中剩餘元素的最大值,放到下標爲1的位置
main(String[] args) {
int[] arr = {10, 3, 5, 7, 9, 2, 4, 6, 8, 1};
int maxIndex = 1;
for (int i = 2; i < arr.length; i++) {
// 如果發現maxIndex對應元素小於下標爲i的元素
if (arr[maxIndex] < arr[i]) {
// 保留i值
maxIndex = i;
}
}
// 假如最大值的下標不是0,可以交換
if (maxIndex != 1) {
int temp = arr[maxIndex];
arr[maxIndex] = arr[1];
arr[1] = temp;
}
System.out.println(Arrays.toString(arr))
}
1.10 再接上一題,找出數組中剩餘元素的最大值,放到下標爲2的位置
main(String[] args) {
int[] arr = {10, 9, 5, 7, 3, 2, 4, 6, 8, 1};
int maxIndex = 2;
for (int i = 3; i < arr.length; i++) {
// 如果發現maxIndex對應元素小於下標爲i的元素
if (arr[maxIndex] < arr[i]) {
// 保留i值
maxIndex = i;
}
}
// 假如最大值的下標不是0,可以交換
if (maxIndex != 2) {
int temp = arr[maxIndex];
arr[maxIndex] = arr[2];
arr[2] = temp;
}
System.out.println(Arrays.toString(arr))
}
1.11 選擇排序算法
/**
* 選擇排序算法
*
* @param array int類型數組
* @return 方法運行成功返回true,方法運行失敗返回false
*/
public static boolean selectSort(int[] array) {
// 參數合法性判斷
if (null == array || array.length == 0) {
System.out.println("Input Parameter is Invalid");
return false;
}
for (int i = 0; i < array.length - 1; i++) {
// i是每一次極值的存放位置,i值也是每一次假設比較位置
int maxIndex = i;
for (int j = i + 1; j < array.length; j++) {
if (array[maxIndex] < array[j]) {
maxIndex = j;
}
}
if (maxIndex != i) {
int temp = array[maxIndex];
array[maxIndex] = array[i];
array[i] = temp;
}
}
return true;
}