黑马程序员技术交流社区

标题: 如何去除数组中值为连续的值为0的项? [打印本页]

作者: 奚华    时间: 2012-11-19 16:01
标题: 如何去除数组中值为连续的值为0的项?
本帖最后由 奚华 于 2012-11-19 16:35 编辑

例如,有下面一个数组
  1. byte[] b ={1,2,3,4,0,7,9,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
复制代码
如何去除这个数组里的最后的那一串0,也就是去除数组中从某一位置开始一直到结尾都是0的串,上面的例子还稍微简单一点,如果数组很大,求实现!
作者: 陶辰    时间: 2012-11-19 18:25
package com.ttc;

public class test8 {

        int j ;
        public static void main(String[] args) {
                // TODO Auto-generated method stub
                test8 tt = new test8();
                int last = 0;
                byte[] b ={1,2,3,4,0,7,9,0,0,0,0,0,0,0,0,0,0,0,0,1,0,0,
                                0,0,0,0,0,0,1,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
                                0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0};
                last = tt.LastArrays(b);
                //System.out.println(last);
                for(int i=0;i<=last;i++){
                        System.out.println("b["+i+"] ="+b[i]);
                }
        }
       
        public int LastArrays(byte[] b){
       
                //System.out.println(b.length);
                for(int i=b.length-1;i>=0;i--){
                        if(b[i]!=0){
                                j = i;
                                break;
                        }
                }
                return j;
        }
}

作者: 奚华    时间: 2012-11-19 18:30
陶辰 发表于 2012-11-19 18:25
package com.ttc;

public class test8 {

谢谢了!!
作者: 奚华    时间: 2012-11-19 18:30
陶辰 发表于 2012-11-19 18:25
package com.ttc;

public class test8 {

谢谢了!!




欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2