码迷,mamicode.com
首页 > 编程语言 > 详细

java基础:现在有如下一个数组: int [] oldArr={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};要求将以上数组中的0项去掉,将不为0的值存入一个新的数组,生成新的数组为:int [] newArr={1,3,4,5,6,6,5,4,7,6,7,5};

时间:2020-04-26 18:55:12      阅读:270      评论:0      收藏:0      [点我收藏+]

标签:static   数组   不为   system   基础   sys   string   java   count   

import java.util.Arrays;

public class HomeWork04_2 {

        public static void main(String[] args){

                int [] oldArr={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};

                int [] newArr= new int[oldArr.length];

                int count=0;

                for (int i = 0; i < oldArr.length; i++) {

                       if(oldArr[i]!=0){

                              newArr[count++]=oldArr[i];

                       }

                 }
                 newArr = Arrays.copyOf(newArr, count);
                 System.out.println(Arrays.toString(newArr));
        }
}

java基础:现在有如下一个数组: int [] oldArr={1,3,4,5,0,0,6,6,0,5,4,7,6,7,0,5};要求将以上数组中的0项去掉,将不为0的值存入一个新的数组,生成新的数组为:int [] newArr={1,3,4,5,6,6,5,4,7,6,7,5};

标签:static   数组   不为   system   基础   sys   string   java   count   

原文地址:https://www.cnblogs.com/C33baby/p/12781301.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!