A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© 奚华 中级黑马   /  2012-11-19 16:01  /  1663 人查看  /  3 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

本帖最后由 奚华 于 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的串,上面的例子还稍微简单一点,如果数组很大,求实现!

3 个回复

倒序浏览
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:25
package com.ttc;

public class test8 {

谢谢了!!
回复 使用道具 举报
陶辰 发表于 2012-11-19 18:25
package com.ttc;

public class test8 {

谢谢了!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马