在Java中追加整数数组元素 [英] Appending Integer array elements in Java

查看:143
本文介绍了在Java中追加整数数组元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个数组,说

int a[]={2,0,1,0,1,1,0,2,1,1,1,0,1,0,1};

我要追加每5相邻元素,并将其分配到一个新的数组 B 长度=(则为a.length / 5) ; ,我想追加5相邻元素,使我有:
INT B〔] = {20101,10211,10101}; 我需要用的长度各种长数组做到这一点,在大多数情况下大于15

I need to append each of the 5 neighboring elements and assign them to a new array b with length=(a.length/5); and i want to append the 5 neighboring elements so that I have: int b[]={20101,10211,10101}; I need to do this for various length arrays, in most cases with length of a being greater than 15.

任何帮助将大大AP preciated,我在Java编程。

Any help would be greatly appreciated, I'm programming in Java.

先谢谢了。

推荐答案

这是pretty简单明了:

It's pretty straighforward:

// Assuming a.length % 5 == 0.
int[] b = new int[a.length / 5];
for (int i = 0; i < a.length; i += 5) {
    b[i/5] = a[i]*10000 + a[i+1]*1000 + a[i+2]*100 + a[i+3]*10 + a[i+4];
}

这篇关于在Java中追加整数数组元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆