在 Java 中附加整数数组元素
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2728476/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Appending Integer array elements in Java
提问by Mohammad Sepahvand
I have an array, say
我有一个数组,说
int a[]={2,0,1,0,1,1,0,2,1,1,1,0,1,0,1};
I need to append each of the 5 neighboring elements and assign them to a new array bwith 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 abeing greater than 15.
我需要追加每5个相邻的元素,并将它们分配到一个新的数组b用length=(a.length/5);,我想在5个相邻的元素添加,使我有:
int b[]={20101,10211,10101};我需要为各种长数组做到这一点,在大多数情况下的长度a是更大超过 15。
Any help would be greatly appreciated, I'm programming in Java.
任何帮助将不胜感激,我正在用 Java 编程。
Thanks in advance.
提前致谢。
回答by Marcelo Cantos
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];
}
回答by Michael Aaron Safyan
This sounds like a homework question, so I won't give you the complete solution, but the basic rundown is:
这听起来像是一个家庭作业问题,所以我不会给你完整的解决方案,但基本的纲要是:
- Compute the length of b: len = a.length / 5
- Construct b with that many elements.
- Initialize an index variable to point to the first element in a
- For each element in b:
- Construct the value for that element from a[idx]...a[idx+4]
- Advance the index into a by 5.
- 计算 b 的长度:len = a.length / 5
- 用那么多元素构造 b 。
- 初始化索引变量以指向 a 中的第一个元素
- 对于 b 中的每个元素:
- 从 a[idx]...a[idx+4] 构造该元素的值
- 将索引推进 5。
Also note that you may need to verify that the input a is actually a multiple of 5 in length.
另请注意,您可能需要验证输入 a 实际上是长度 5 的倍数。
回答by polygenelubricants
This works with (a.length % 5) != 0, and keeps leading zeroes (by storing digits into String).
这适用于(a.length % 5) != 0,并保持前导零(通过将数字存储到String)。
int a[]={2,0,1,0,1,1,0,2,1,1,1,0,1,0,1,0,0,7};
final int N = 5;
String b[] = new String[(a.length + N - 1)/ N];
StringBuilder sb = new StringBuilder(N);
int x = 0;
for (int i = 0; i < b.length; i++) {
sb.setLength(0);
for (int k = 0; k < N && x < a.length; k++) {
sb.append(a[x++]);
}
b[i] = sb.toString();
}
System.out.println(java.util.Arrays.toString(b));
// prints "[20101, 10211, 10101, 007]"
Alternately, you can also use regex:
或者,您也可以使用正则表达式:
String[] arr =
java.util.Arrays.toString(a)
.replaceAll("\D", "")
.split("(?<=\G.{5})");
System.out.println(java.util.Arrays.toString(arr));
// prints "[20101, 10211, 10101, 007]"
Basically this uses Arrays.toString(int[])to append all digits into one long String, then removes all non-digits \D, then uses \G-anchored lookbehind to split every .{5}
基本上,这用于Arrays.toString(int[])将所有数字附加到一个 long 中String,然后删除所有非数字\D,然后使用\G-anchored lookbehind 拆分每个.{5}
回答by miku
Naive approach.
天真的方法。
import java.util.ArrayList;
/* Naive approach */
public class j2728476 {
public static void main(String[] args) {
int a[] = {2,0,1,0,1,1,0,2,1,1,1,0,1,0,1};
ArrayList<String> al = new ArrayList<String>();
String s = "";
for (int i = 0; i < a.length; i++) {
if (i % 5 == 0 && i != 0) {
al.add(s);
s = "" + a[i];
} else {
s += a[i];
}
}
al.add(s);
for (String t : al) {
// convert values to ints ...
System.out.println(t);
}
}
}
Will print:
将打印:
20101
10211
10101
回答by Deepak Lamichhane
import java.util.Arrays;
public class MainClass {
public static void main(String args[]) throws Exception {
int array[] = { 2, 5, -2, 6, -3, 8, 0, -7, -9, 4 };
Arrays.sort(array);
printArray("Sorted array", array);
int index = Arrays.binarySearch(array, 1);
System.out.println("Didn't find 1 @ "
+ index);
int newIndex = -index - 1;
array = insertElement(array, 1, newIndex);
printArray("With 1 added", array);
}
private static void printArray(String message, int array[]) {
System.out.println(message
+ ": [length: " + array.length + "]");
for (int i = 0; i < array.length; i++) {
if (i != 0){
System.out.print(", ");
}
System.out.print(array[i]);
}
System.out.println();
}
private static int[] insertElement(int original[],
int element, int index) {
int length = original.length;
int destination[] = new int[length + 1];
System.arraycopy(original, 0, destination, 0, index);
destination[index] = element;
System.arraycopy(original, index, destination, index
+ 1, length - index);
return destination;
}
}
It will print
它会打印
Sorted array: [length: 10]
-9, -7, -3, -2, 0, 2, 4, 5, 6, 8
Didn't find 1 @ -6
With 1 added: [length: 11]
-9, -7, -3, -2, 0, 1, 2, 4, 5, 6, 8

