首页 > 编程语言 > 详细

Java 数组 之 一维数组 删除 元素

时间:2018-06-16 11:28:28      阅读:211      评论:0      收藏:0      [点我收藏+]

http://www.verejava.com/?id=16992656053526

/*
题目: 删除scores数组索引index位置的值
思路:
    1. 创建一个临时比scores 小1的临时数组tempArray
    2. 将index前面的数据复制到tempArray前面
    3. 将index后面的数组依次复制到 tempArray 后面
    4. 将tempArray地址指针引用赋值给 scores
    5. 打印输出scores
*/
import java.util.Scanner;
public class ArrayDelete
{
    public static void main(String[] args)
    {
        //一维数组的定义和初始化
        int[] scores={90,70,50,80,60,85};
        System.out.println("请输入要删除的索引index :");
        Scanner in=new Scanner(System.in);
        int index=in.nextInt();

        //1. 创建一个临时比scores 小1的临时数组tempArray
        int[] tempArray=new int[scores.length-1];
        //2. 将index前面的数据复制到tempArray前面
        //3. 将index后面的数组依次复制到 tempArray 后面
        for(int i=0;i<scores.length;i++)
        {
            if(i<index)
                tempArray[i]=scores[i];
            if(i>index)
                tempArray[i-1]=scores[i];
        }
        //4. 将tempArray地址指针引用赋值给 scores
        scores=tempArray;
        //5.打印输出scores
        for(int i=0;i<scores.length;i++)
        {
            System.out.print(scores[i]+",");
        }

    }
}

http://www.verejava.com/?id=16992656053526

Java 数组 之 一维数组 删除 元素

原文:https://www.cnblogs.com/verejava/p/9189757.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!