eclipse 添加背景代码_Eclipse

这篇博客详细介绍了如何使用Eclipse进行数组操作,包括获取Eclipse、安装准备、创建Java项目。此外,还深入讲解了数组中最大值和最小值下标查找、元素插入与删除等难点,并逐步解析了选择排序算法的过程。最后,提供了Eclipse常用快捷键和项目导入的指南。
摘要由CSDN通过智能技术生成

Eclipse使用,数组操作

1. Eclipse使用

1.1 Eclipse获取

Eclipse官网

Eclipse下载页面

f4591dc7fa5b0c37a36510b217094963.png

f4591dc7fa5b0c37a36510b217094963.png

1.2 Eclipse安装和准备

1. 解压Eclipse压缩包到一个非中文,非C盘路径下
    eclipse-jee-2020-03-R-incubation-win32-x86_64.zip 
    解压之后 ==> eclipse

2. eclipse文件夹中找到eclipse.exe 发送到桌面快捷方式

3. 打开Eclipse.exe

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

f4591dc7fa5b0c37a36510b217094963.png
5. Eclipse页面概述

f4591dc7fa5b0c37a36510b217094963.png

1.3 在Eclipse中创建第一个Java Project Java项目

1. File -> New -> Java Project
    Alt + Shift + N Or Ctrl + N

f4591dc7fa5b0c37a36510b217094963.png
2. 创建Java Project配置页面

f4591dc7fa5b0c37a36510b217094963.png
3. 创建包

f4591dc7fa5b0c37a36510b217094963.png
4. 创建包配置

f4591dc7fa5b0c37a36510b217094963.png
5. 创建Java程序

f4591dc7fa5b0c37a36510b217094963.png
6. 配置类名

f4591dc7fa5b0c37a36510b217094963.png
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);
    }
    /*
     * a. 找出数组中最大值的下标位置
     * 方法分析:
     *      固定格式:
     *          public static 不要问
     *      返回值类型:
     *          int 返回值数据是类型
     *      方法名:
     *          maxIndexOf 最大值下标位置
     *      形式参数列表:
     *          int[] arr 这里需要在一个int类型数组中找出
     * 
     * 方法声明:
     *      public static int maxIndexOf(int[] arr)
     */

    /**
     * 找出数组中最大值的下标位置
     * 
     * @param arr 查询最大值下标位置的数组
     * @return 返回最大值所在的下标位置,int类型
     */
    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);
    }

    /*
     * b. 找出数组中最小值的下标位置
     * 方法分析:
     *      固定格式:
     *          public static
     *      返回值类型:
     *          int 
     *      方法名:
     *          minIndexOf
     *      形式参数列表:
     *          (int[] arr)
     * 方法声明:
     *      public static int minIndexOf(int[] array)
     */

    /**
     * 找出数组中最小值下标位置
     * 
     * @param array 查询操作的数组
     * @return 最小值的下标位置
     */
    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
        需求
            在该数组中的指定下标位置放入指定元素

f4591dc7fa5b0c37a36510b217094963.png
代码运行中是否有需要考虑的异常情况?
    越界问题
        用户指定的下标位置,超出的有效位置

需要在代码中进行参数合法性判定!!!
方法分析:
    固定格式:
        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;

/**
 * 在数组中指定下标位置添加元素
 * @author Anonymous
 *
 */
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("方法运行失败!!!");
        }
    }

    /**
     * 在数组arr中指定下标位置,添加指定元素
     * 
     * @param arr    添加元素是数组
     * @param index  指定添加数据的下标位置
     * @param insert 指定添加的数据
     * @return 方法运行成功完成添加操作,返回true,否则返回false
     */
    public static boolean add(int[] arr, int index, int insert) {
        // 参数合法性判断
        if (index < 0 || index > arr.length - 1) {
            System.out.println("Input Parameter is Invalid!");
            // 用户传入参数不合法,返回false,方法运行失败,终止方法运行
            return false;
            // System.exit(0) 退出整个程序
        }

        /*
         * 数据是从最后一个有效元素下标位置开始整体向后移动,移动结束位置是
         * 插入元素的下标位置。
         * 
         * 先完成一个大概的循环要求,然后根据推演结果完成精细加工过程
         */
        /*
         * arr.length - 1 ==> 9
         * index = 5;
         * 
         * arr[9] = arr[9 - 1];
         * arr[8] = arr[8 - 1];
         * arr[7] = arr[7 - 1];
         * arr[6] = arr[6 - 1];
         * 
         * for循环满足要求
         */
        for (int i = arr.length - 1; i > index; i--) {
            arr[i] = arr[i - 1];
        }

        // 用户指定位置添加指定元素
        arr[index] = insert;

        // 运行成功返回true
        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占位!!!

f4591dc7fa5b0c37a36510b217094963.png
方法分析:
    固定格式:
        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("方法运行失败!!!");
        }
    }

    /**
     * 删除数组中指定下标位置的元素
     * 
     * @param arr 需要进行参数操作的数组
     * @param index 指定删除的下标位置
     * @return 方法运行成功返回true,否则返回false
     */
    public static boolean remove(int[] arr, int index) {
        // 参数合法性判断
        if (index < 0 || index > arr.length - 1) {
            System.out.println("Input Parameter is Invalid!");

            // 参数不合法,方法运行失败!!!
            return false;
        }

        /*
         * 删除操作就是从删除位置开始,到最大下标结束
         * 
         * index = 5;
         * arr.length = 10;
         * 
         * arr[5] = arr[5 + 1];
         * arr[6] = arr[6 + 1];
         * arr[7] = arr[7 + 1];
         * arr[8] = arr[8 + 1];
         * 
         * arr[9] = arr[9 + 1]; 错误
         * 所以 arr.length - 1 作为终止条件
         */
        for (int i = index; i < arr.length - 1; i++) {
            arr[i] = arr[i + 1];
        }

        // 最后一个赋值为0,占位符
        arr[arr.length - 1] = 0;

        return true;
    }
}

3. 选择排序算法推导

3.1 找出数组中最大值,和下标为0的元素互换位置

int[] arr = {1, 3, 5, 7, 9, 2, 4, 6, 8, 10};
/*                                          
 * 1. 找出数组中的最大值下标位置                         
 */                                         
int index = 0;                              
for (int i = 1; i < arr.length; i++) {      
    if (arr[index] < arr[i]) {              
        index = i;                          
    }                                       
}                                           

/*                                          
 * 2. 和下标为0的元素交换位置                          
 * 如果说下标为0的元素就是最大值,这里不需要交换                  
 */                                         
if (index != 0) {                           
    int temp = arr[0];                      
    arr[0] = arr[index];                    
    arr[index] = temp;                      
}

3.2 接上一题, 找出数组中剩余数据最大值,和下标为1的元素互换位置

int index = 1;                                       
/*                                               
 * 1. 从下标为1的位置开始找出最大值的下标位置                       
 */                                              
for (int i = 2; i < arr.length; i++) {           
    if (arr[index] < arr[i]) {                   
        index = i;                               
    }                                            
}                                                

/*                                               
 * 2. 交换位置                                       
 */                                              
if (index != 1) {                                
    int temp = arr[1];                           
    arr[1] = arr[index];                         
    arr[index] = temp;                           
}

3.3 接上一题, 找出数组中最大值,和下标为2的元素互换位置

int index = 2;                                       
/*                                               
 * 1. 从下标为1的位置开始找出最大值的下标位置                       
 */                                              
for (int i = 3; i < arr.length; i++) {           
    if (arr[index] < arr[i]) {                   
        index = i;                               
    }                                            
}                                                

/*                                               
 * 2. 交换位置                                       
 */                                              
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;

/**
 * 选择排序算法
 * 
 * @author Anonymous
 *
 */
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));
    }

    /**
     * 选择排序算法
     * 
     * @param arr int类型数组
     */
    public static void selectSort(int[] arr) {
        // 外层循环,控制需要进行核心操作的次数,次数是数据量 - 1
        for (int i = 0; i < arr.length - 1; i++) {

            // 从 i值位置进行搜索
            int index = i;

            // 找出符合要求的极值下标位置
            for (int j = i + 1; j < arr.length; j++) {

                // if 之后是选择排序排序算法的核心
                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 删除当前行代码

【Eclipse导入项目】

f4591dc7fa5b0c37a36510b217094963.png

f4591dc7fa5b0c37a36510b217094963.png

f4591dc7fa5b0c37a36510b217094963.png
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值