java - Combination of elements of multiple arrays

Java - Combination of elements of multiple arrays

To generate combinations of elements from multiple arrays in Java, you can use recursion. Here's how you can implement it:

import java.util.ArrayList; import java.util.Arrays; import java.util.List; public class Main { public static void main(String[] args) { int[] array1 = {1, 2}; int[] array2 = {3, 4}; int[] array3 = {5, 6}; List<List<Integer>> combinations = generateCombinations(array1, array2, array3); for (List<Integer> combination : combinations) { System.out.println(combination); } } private static List<List<Integer>> generateCombinations(int[]... arrays) { List<List<Integer>> result = new ArrayList<>(); generateCombinationsHelper(result, new ArrayList<>(), arrays, 0); return result; } private static void generateCombinationsHelper(List<List<Integer>> result, List<Integer> current, int[][] arrays, int depth) { if (depth == arrays.length) { result.add(new ArrayList<>(current)); return; } for (int value : arrays[depth]) { current.add(value); generateCombinationsHelper(result, current, arrays, depth + 1); current.remove(current.size() - 1); } } } 

In this example:

  • We have three arrays (array1, array2, and array3) containing integer elements.
  • The generateCombinations method takes a variable number of arrays as input and returns a list of lists representing all combinations of elements from these arrays.
  • The generateCombinationsHelper method is a recursive helper function that generates combinations by iterating over each element in each array.
  • The combinations are stored in a list of lists (result), where each inner list represents one combination.
  • Finally, we print each combination to the console.

You can modify this code according to your specific requirements, such as handling different types of elements or generating combinations of a specific size.

Examples

  1. "Java combine arrays into one array"

    • Description: This query seeks a solution to merge multiple arrays into a single array in Java.
    • Code:
      public static <T> T[] combineArrays(T[]... arrays) { int totalLength = 0; for (T[] array : arrays) { totalLength += array.length; } T[] result = (T[]) Array.newInstance(arrays[0].getClass().getComponentType(), totalLength); int currentIndex = 0; for (T[] array : arrays) { System.arraycopy(array, 0, result, currentIndex, array.length); currentIndex += array.length; } return result; } 
  2. "Java concatenate arrays"

    • Description: This query looks for a way to concatenate or join multiple arrays together in Java.
    • Code:
      public static <T> T[] concatenateArrays(T[]... arrays) { List<T> resultList = new ArrayList<>(); for (T[] array : arrays) { Collections.addAll(resultList, array); } return resultList.toArray(Arrays.copyOf(arrays[0], resultList.size())); } 
  3. "Java merge arrays without duplicates"

    • Description: This query aims to combine arrays while ensuring that duplicate elements are not included in the resulting array.
    • Code:
      public static <T> T[] mergeArraysNoDuplicates(T[]... arrays) { Set<T> resultSet = new HashSet<>(); for (T[] array : arrays) { resultSet.addAll(Arrays.asList(array)); } return resultSet.toArray(Arrays.copyOf(arrays[0], resultSet.size())); } 
  4. "Java combine arrays without duplicates"

    • Description: Similar to the previous query, this one specifically focuses on combining arrays while eliminating duplicates.
    • Code:
      public static <T> T[] combineArraysNoDuplicates(T[]... arrays) { Set<T> resultSet = new LinkedHashSet<>(); for (T[] array : arrays) { Collections.addAll(resultSet, array); } return resultSet.toArray(Arrays.copyOf(arrays[0], resultSet.size())); } 
  5. "Java merge arrays with duplicates"

    • Description: This query seeks a solution to merge arrays while retaining duplicate elements in the resulting array.
    • Code:
      public static <T> T[] mergeArraysWithDuplicates(T[]... arrays) { List<T> resultList = new ArrayList<>(); for (T[] array : arrays) { Collections.addAll(resultList, array); } return resultList.toArray(Arrays.copyOf(arrays[0], resultList.size())); } 
  6. "Java merge arrays preserving order"

    • Description: This query aims to merge arrays while maintaining the order of elements from the original arrays.
    • Code:
      public static <T> T[] mergeArraysPreservingOrder(T[]... arrays) { List<T> resultList = new ArrayList<>(); for (T[] array : arrays) { Collections.addAll(resultList, array); } return resultList.toArray(Arrays.copyOf(arrays[0], resultList.size())); } 
  7. "Java combine arrays with null elements"

    • Description: This query addresses the scenario where arrays may contain null elements and need to be merged.
    • Code:
      public static <T> T[] combineArraysWithNulls(T[]... arrays) { List<T> resultList = new ArrayList<>(); for (T[] array : arrays) { for (T element : array) { resultList.add(element); } } return resultList.toArray(Arrays.copyOf(arrays[0], resultList.size())); } 
  8. "Java merge arrays with custom objects"

    • Description: This query looks for a method to merge arrays containing custom objects in Java.
    • Code:
      public static CustomObject[] mergeCustomArrays(CustomObject[]... arrays) { List<CustomObject> resultList = new ArrayList<>(); for (CustomObject[] array : arrays) { Collections.addAll(resultList, array); } return resultList.toArray(new CustomObject[0]); } 
  9. "Java concatenate arrays of different types"

    • Description: This query explores how to concatenate arrays of different data types in Java.
    • Code:
      public static Object[] concatenateArraysOfDifferentTypes(Object[]... arrays) { List<Object> resultList = new ArrayList<>(); for (Object[] array : arrays) { Collections.addAll(resultList, array); } return resultList.toArray(); } 
  10. "Java combine arrays using streams"

    • Description: This query seeks a solution to merge arrays using Java streams.
    • Code:
      public static <T> T[] combineArraysUsingStreams(T[]... arrays) { return Arrays.stream(arrays) .flatMap(Arrays::stream) .toArray(size -> Arrays.copyOf(arrays[0], size)); } 

More Tags

sys-refcursor 3d-reconstruction println prettier rangeslider systemjs expo darwin asmx visual-studio-2010

More Programming Questions

More Retirement Calculators

More General chemistry Calculators

More Genetics Calculators

More Housing Building Calculators