Java 将整数数组推送到ArrayList
我陷入了将整数数组的序列推送到arraylist的非常基本的方法中。 我试图改变多基因润滑油对这个问题的解决方案,这样我就不会打印它们,而是将它们推到一个数组列表中 我的代码: 但是,当我将ArrayList的类型从String更改为Integer[]时,我得到了冗余的输出 更改代码 有人能帮我指出我做错了什么吗 谢谢, SarathJava 将整数数组推送到ArrayList,java,arraylist,combinations,Java,Arraylist,Combinations,我陷入了将整数数组的序列推送到arraylist的非常基本的方法中。 我试图改变多基因润滑油对这个问题的解决方案,这样我就不会打印它们,而是将它们推到一个数组列表中 我的代码: 但是,当我将ArrayList的类型从String更改为Integer[]时,我得到了冗余的输出 更改代码 有人能帮我指出我做错了什么吗 谢谢, Sarath公共类测试{ 静态数组列表组合; 公共静态void main(字符串参数[]){ 整数[]a3={1,2,3,4,5}; 梳子(a3,2); } 公共静态空梳(整数
公共类测试{
静态数组列表组合;
公共静态void main(字符串参数[]){
整数[]a3={1,2,3,4,5};
梳子(a3,2);
}
公共静态空梳(整数[]项,整数k){
数组。排序(项);
组合=新的ArrayList();
ArrayList c1=新的ArrayList();
c1=kcomb(项目,0,k,新整数[k]);
System.out.println(“从梳”);
对于(整数[]x:c1){
System.out.println(Arrays.toString(x));
}
}
公共静态数组列表kcomb(整数[]项,整数n,整数k,
整数[]arr){
如果(k==0){
组合。添加(arr);
}否则{
对于(int i=n;i有您的错误:组合。添加(arr);
您总是在同一个数组上工作arr
请记住,数组是对象并具有引用。您可以将相同的数组arr
引用保存到ArrayList中,并在之后不断更改数组值。每次处理同一数组时,您都会获得所有其他组合的最后一个组合值
因此,在将其添加到ArrayList之前,您需要克隆arr
,以获取新引用。由于字符串是不可变的,因此代码以前一直在工作,因为每个字符串都有自己的引用。问题是,您只创建了一个整数[]数组-每次调用kcomb时都会重复使用它,因此在过程结束时,同一数组已多次添加到列表中,但数组的内容只是最后一个组合。此外,您不需要为此使用整数[]-int[]你确定它的ArrayList
不是ArrayList
@codeMan吗,当我们得到a3中的整数组合时,它应该是ArrayList。我试图存储在ArrayList中的是多组组合。谢谢。更改组合。添加(arr);到组合。添加((Integer[])arr.clone();
public class Test {
static ArrayList<String> combinations;
public static void main(String args[]) {
Integer[] a3 = { 1, 2, 3, 4, 5 };
comb(a3, 2);
}
public static void comb(Integer[] items, int k) {
Arrays.sort(items);
combinations = new ArrayList<String>();
ArrayList<String> c1 = new ArrayList<String>();
c1 = kcomb(items, 0, k, new Integer[k]);
System.out.println("from comb");
for (String x : c1) {
System.out.println(x);
}
}
public static ArrayList<String> kcomb(Integer[] items, int n, int k,
Integer[] arr) {
if (k == 0) {
combinations.add(Arrays.toString(arr));
} else {
for (int i = n; i <= items.length - k; i++) {
arr[arr.length - k] = items[i];
kcomb(items, i + 1, k - 1, arr);
}
}
return combinations;
}
}
from comb
[1, 2]
[1, 3]
[1, 4]
[1, 5]
[2, 3]
[2, 4]
[2, 5]
[3, 4]
[3, 5]
[4, 5]
public class Test {
static ArrayList<Integer[]> combinations;
public static void main(String args[]) {
Integer[] a3 = { 1, 2, 3, 4, 5 };
comb(a3, 2);
}
public static void comb(Integer[] items, int k) {
Arrays.sort(items);
combinations = new ArrayList<Integer[]>();
ArrayList<Integer[]> c1 = new ArrayList<Integer[]>();
c1 = kcomb(items, 0, k, new Integer[k]);
System.out.println("from comb");
for (Integer[] x : c1) {
System.out.println(Arrays.toString(x));
}
}
public static ArrayList<Integer[]> kcomb(Integer[] items, int n, int k,
Integer[] arr) {
if (k == 0) {
combinations.add(arr);
} else {
for (int i = n; i <= items.length - k; i++) {
arr[arr.length - k] = items[i];
kcomb(items, i + 1, k - 1, arr);
}
}
return combinations;
}
}
from comb
[4, 5]
[4, 5]
[4, 5]
[4, 5]
[4, 5]
[4, 5]
[4, 5]
[4, 5]
[4, 5]
[4, 5]
public class Test {
static ArrayList<Integer[]> combinations;
public static void main(String args[]) {
Integer[] a3 = { 1, 2, 3, 4, 5 };
comb(a3, 2);
}
public static void comb(Integer[] items, int k) {
Arrays.sort(items);
combinations = new ArrayList<Integer[]>();
ArrayList<Integer[]> c1 = new ArrayList<Integer[]>();
c1 = kcomb(items, 0, k, new Integer[k]);
System.out.println("from comb");
for (Integer[] x : c1) {
System.out.println(Arrays.toString(x));
}
}
public static ArrayList<Integer[]> kcomb(Integer[] items, int n, int k,
Integer[] arr) {
if (k == 0) {
combinations.add(arr);
} else {
for (int i = n; i <= items.length - k; i++) {
Integer[] arr1 = new Integer[arr.length];
System.arraycopy(arr, 0, arr1, 0, arr.length);
arr1[arr.length - k] = items[i];
kcomb(items, i + 1, k - 1, arr1);
}
}
return combinations;
}
}