Eclipse使用,数组操作
1. Eclipse使用
1.1 Eclipse获取
Eclipse官网
Eclipse下载页面


1.2 Eclipse安装和准备
1. 解压Eclipse压缩包到一个非中文,非C盘路径下
eclipse-jee-2020-03-R-incubation-win32-x86_64.zip
解压之后 ==> eclipse
2. eclipse文件夹中找到eclipse.exe 发送到桌面快捷方式
3. 打开Eclipse.exe

4. 设置Eclipse工作环境,Eclipse软件的右上角

5. Eclipse页面概述

1.3 在Eclipse中创建第一个Java Project Java项目
1. File -> New -> Java Project
Alt + Shift + N Or Ctrl + N

2. 创建Java Project配置页面

3. 创建包

4. 创建包配置

5. 创建Java程序

6. 配置类名

7. Ctrl + +/-
调整代码区字号
2. 作业讲解
2.1 找出数组中最大值的下标位置
package com.qfedu.a.homework;
public class HomeWork1 {
public static void main(String[] args) {
int[] array = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
int maxIndex = maxIndexOf(array);
System.out.println("最大值下标位置:" + maxIndex);
}
public static int maxIndexOf(int[] arr) {
int maxIndex = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[maxIndex] < arr[i]) {
maxIndex = i;
}
}
return maxIndex;
}
}
2.2 找出数组中最小值的下标位置
package com.qfedu.a.homework;
public class HomeWork2 {
public static void main(String[] args) {
int[] array = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
int minIndex = minIndexOf(array);
System.out.println(minIndex);
}
public static int minIndexOf(int[] array) {
int minIndex = 0;
for (int i = 1; i < array.length; i++) {
if (array[minIndex] > array[i]) {
minIndex = i;
}
}
return minIndex;
}
}
2.3 在指定位置插入指定元素【难点】
c. 在指定位置插入指定元素【难点】
存在一个数组,数组中的元素为
int[] array = {1, 3, 5, 7, 9, 11, 13, 15, 17, 0};
要求
1. 0是无效元素,仅占位使用
2. 当前数组中【有效元素】个数为9
需求
在该数组中的指定下标位置放入指定元素

代码运行中是否有需要考虑的异常情况?
越界问题
用户指定的下标位置,超出的有效位置
需要在代码中进行参数合法性判定!!!
方法分析:
固定格式:
public static
返回值类型:
void:
OK选择!!!
int:
操作成功返回1,失败返回-1
boolean: [选择]
添加成功返回true,运行失败返回false
方法名:
add 这里是一个添加操作
形式参数列表:
1. 添加数据的数组
2. 指定添加的下标位置
3. 指定添加的数据
(int[] arr, int index, int insert)
方法声明:
public static boolean add(int[] arr, int index, int insert)
package com.qfedu.a.homework;
public class HomeWork3 {
public static void main(String[] args) {
int[] array = {1, 3, 5, 7, 9, 11, 13, 15, 17, 0};
boolean ret = add(array, 5, 100);
if (ret) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
} else {
System.out.println("方法运行失败!!!");
}
}
public static boolean add(int[] arr, int index, int insert) {
if (index < 0 || index > arr.length - 1) {
System.out.println("Input Parameter is Invalid!");
return false;
}
for (int i = arr.length - 1; i > index; i--) {
arr[i] = arr[i - 1];
}
arr[index] = insert;
return true;
}
}
2.4 删除数组中的指定下标的元素【难点】
d. 删除数组中的指定下标的元素【难点】
存在一个数组,数组中的元素为
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: 检测方法方法运行的状态,运行成功返回true,运行失败返回false
方法名:
remove √
delete
形式参数列表:
1. int[] arr 需要删除数据的数组
2. int index 指定删除的下标位置
这里需要对于下标位置进行判断,必须是一个合法的下标位置
(int[] arr, int index)
方法声明:
public static boolean remove(int[] arr, int index)
package com.qfedu.a.homework;
public class HomeWork4 {
public static void main(String[] args) {
int[] array = {1, 3, 5, 7, 9, 11, 13, 15, 17, 19};
boolean ret = remove(array, 5);
if (ret) {
for (int i = 0; i < array.length; i++) {
System.out.print(array[i] + " ");
}
System.out.println();
} else {
System.out.println("方法运行失败!!!");
}
}
public static boolean remove(int[] arr, int index) {
if (index < 0 || index > arr.length - 1) {
System.out.println("Input Parameter is Invalid!");
return false;
}
for (int i = index; i < arr.length - 1; i++) {
arr[i] = arr[i + 1];
}
arr[arr.length - 1] = 0;
return true;
}
}
3. 选择排序算法推导
3.1 找出数组中最大值,和下标为0的元素互换位置
int[] arr = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
int index = 0;
for (int i = 1; i < arr.length; i++) {
if (arr[index] < arr[i]) {
index = i;
}
}
if (index != 0) {
int temp = arr[0];
arr[0] = arr[index];
arr[index] = temp;
}
3.2 接上一题, 找出数组中剩余数据最大值,和下标为1的元素互换位置
int index = 1;
for (int i = 2; i < arr.length; i++) {
if (arr[index] < arr[i]) {
index = i;
}
}
if (index != 1) {
int temp = arr[1];
arr[1] = arr[index];
arr[index] = temp;
}
3.3 接上一题, 找出数组中最大值,和下标为2的元素互换位置
int index = 2;
for (int i = 3; i < arr.length; i++) {
if (arr[index] < arr[i]) {
index = i;
}
}
if (index != 2) {
int temp = arr[2];
arr[2] = arr[index];
arr[index] = temp;
}
3.4 完成选择排序算法
选择排序算法核心要求
1. 找出符合要求的数据下标位置
2. 从下标0位置开始,递增,数据交换
package com.qfedu.b.sort;
import java.util.Arrays;
public class Demo2 {
public static void main(String[] args) {
int[] arr = { 1, 3, 5, 7, 9, 2, 4, 6, 8, 10 };
selectSort(arr);
System.out.println(Arrays.toString(arr));
}
public static void selectSort(int[] arr) {
for (int i = 0; i < arr.length - 1; i++) {
int index = i;
for (int j = i + 1; j < arr.length; j++) {
if (arr[index] < arr[j]) {
index = j;
}
}
if (index != i) {
int temp = arr[i];
arr[i] = arr[index];
arr[index] = temp;
}
}
}
}
3.5 总结
1. 从推导 ==> 方法
2. 学会总结,找出规律,善于发现规律,需要始终保存一个封装的思想!!!
【Eclipse 常用快捷键】
1. Alt + / 代码快速补齐
2. Ctrl + D 删除当前行代码