gpt4 book ai didi

java - 如何在 Java 中获取 Integer arrayList 的组合集合

转载 作者:行者123 更新时间:2023-12-02 11:05:32 25 4
gpt4 key购买 nike

我的目标是在具有固定预定义长度的 ArrayList 中找到所有可能的项目组合。例如,如果我的 ArrayList 名为 arr 并包含 <1, 2, 3>那么预定义大小 r = 2 的所需输出将是:

<1,2>
<1,3>
<2,3>

这是我发现的打印所需输出的代码。我的问题是我需要定义一个返回值类型 ArrayList 来保存该方法的输出。另外,我的输入类型也是ArrayList<Integer> ,而不是数组,这对我来说变得更加复杂,因为我首先需要将值转换为原始类型 int。

import java.io.*;

class Permutation {

/* arr[] ---> Input Array
data[] ---> Temporary array to store current combination
start & end ---> Staring and Ending indexes in arr[]
index ---> Current index in data[]
r ---> Size of a combination to be printed */
static void combinationUtil(int arr[], int data[], int start,
int end, int index, int r)
{
// Current combination is ready to be printed, print it
if (index == r)
{
for (int j=0; j<r; j++)
System.out.print(data[j]+" ");
System.out.println("");
return;
}

// replace index with all possible elements. The condition
// "end-i+1 >= r-index" makes sure that including one element
// at index will make a combination with remaining elements
// at remaining positions
for (int i=start; i<=end && end-i+1 >= r-index; i++)
{
data[index] = arr[i];
combinationUtil(arr, data, i+1, end, index+1, r);
}
}

// The main function that prints all combinations of size r
// in arr[] of size n. This function mainly uses combinationUtil()
static void printCombination(int arr[], int n, int r)
{
// A temporary array to store all combination one by one
int data[]=new int[r];

// Print all combination using temprary array 'data[]'
combinationUtil(arr, data, 0, n-1, 0, r);
}

/*Driver function to check for above function*/
public static void main (String[] args) {
int arr[] = {1, 2, 3, 4, 5};
int r = 3;
int n = arr.length;
printCombination(arr, n, r);
}
}

/* This code is contributed by Devesh Agrawal */

最佳答案

ArrayList 由数组内部支持,因此将当前基于 array 的实现转换为 ArrayList 应该是合理的。在数组中,您使用[]运算符来索引数组中的元素,并使用ArrayList进行并行操作是getset 。您也可能想阅读Autoboxing and Unboxing 。使用列表的可能实现:

static void combinationUtil(List<Integer> list, List<Integer> data, int start, int end, int index, int r) {
// Current combination is ready to be printed, print it
if (index == r) {
for (int j = 0; j < r; j++)
System.out.print(data.get(j) + " ");
System.out.println("");
return;
}

// replace index with all possible elements. The condition
// "end-i+1 >= r-index" makes sure that including one element
// at index will make a combination with remaining elements
// at remaining positions
for (int i = start; i <= end && end - i + 1 >= r - index; i++) {
data.set(index, list.get(i));
combinationUtil(list, data, i + 1, end, index + 1, r);
}
}

// The main function that prints all combinations of size r
// in list of size n. This function mainly uses combinationUtil()
static void printCombination(List<Integer> list, int n, int r) {
// A temporary array to store all combination one by one
List<Integer> data = new ArrayList<>(Collections.nCopies(r, 0));

// Print all combination using temporary array 'data'
combinationUtil(list, data, 0, n - 1, 0, r);
}

public static void main(String[] args) {
List<Integer> list = Arrays.asList(1, 2, 3, 4, 5);
int r = 3;
int n = list.size();
printCombination(list, n, r);
}

关于java - 如何在 Java 中获取 Integer arrayList 的组合集合,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51006323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com