如何获取数组中指定的数据

如:数组 a[]=[1,2,3,4,5,6,7,8];想要获取 第2个数到第六个数 即 [2,3,4,5,6]

从数组中获取指定数量的数据值,然后打包,这个数组中的的长度是未知的。
例如:数组中有22个数据值,指定每个包为5条数组,则输入如下:
第一个包:01 | 02 | 03 | 04 | 05
第二个包:06 | 07 | 08 | 09 | 10
第三个包:11 | 12 | 13 | 14 | 15
第四个包:16 | 17 | 18 | 19 | 20
第五个包:21 | 22

大致的方法体如下:(剩余的两条未实现打包,怎样将剩余的2条计算并打包)
protected void Test() {

//每行输出的数量
const int PACKAGE = 5;

//该数组的元素数量是未知的,当前假设有22个数据值。
String[] arr = new String[22] { "01", "02", "03", "04", "05", "06", "07", "08", "09", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22" };
int count = arr.Length;
int n = 0;
String[] a = new String[PACKAGE];
for (int i = 0; i < count; i++) {
n++;
a[n - 1] = arr[i];
if (n == PACKAGE) {
Console.WriteLine(String.Join("|", a) + "\r\n");
n = 0;
a = new String[PACKAGE];
}
}
}
/*

想要的输出结如下:(数组中有N个元素,按照 PACKAGE 的值换行)
01 | 02 | 03 | 04 | 05
06 | 07 | 08 | 09 | 10
11 | 12 | 13 | 14 | 15
16 | 17 | 18 | 19 | 20
21 | 22

*/
温馨提示:答案为网友推荐,仅供参考
第1个回答  2014-12-11
int[] result = new int[5];
System.arraycopy(a, 2, result, 0, result.length);

追问

能详细点,解释一下吗?我的目的是取出来赋给其他数组

追答

这个就是从第2个索引到第六个索引被复制给了result数组,这个就是你要的

本回答被提问者和网友采纳
第2个回答  2014-12-11
public void test(int start ,int end){
for(int i = start;i<=end;i++){
System.out.print(a[i]+"\t");
}
}
第3个回答  2014-12-11
for(int i=1;i<6;i++){
System.out.println(a[i]);
}追问

朋友这个不行的,我要的是指定的

第4个回答  2014-12-11
什么语言?追问

java

相似回答