Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package com.neel.task1;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.List;
- public class FlatIntegerArray {
- /**
- * @param inputArray
- * @return
- * @throws IllegalArgumentException
- */
- public static Integer[] flatten(Object[] inputArray) throws IllegalArgumentException {
- if (inputArray == null)
- return null;
- List<Integer> flatList = new ArrayList<Integer>();
- for (Object element : inputArray) {
- if (element instanceof Integer) {
- flatList.add((Integer) element);
- } else if (element instanceof Object[]) {
- flatList.addAll(Arrays.asList(flatten((Object[]) element)));
- } else {
- throw new IllegalArgumentException("Input must be an array of Integers or nested arrays of Integers");
- }
- }
- return flatList.toArray(new Integer[flatList.size()]);
- }
- /**
- * @param inputArray
- * @return
- * @throws IllegalArgumentException
- */
- public static ArrayList<Integer> flattenList(Object[] inputArray) throws IllegalArgumentException {
- if (inputArray == null)
- return null;
- List<Integer> flatList = new ArrayList<Integer>();
- for (Object element : inputArray) {
- if (element instanceof Integer) {
- flatList.add((Integer) element);
- } else if (element instanceof Object[]) {
- flatList.addAll(Arrays.asList(flatten((Object[]) element)));
- } else {
- throw new IllegalArgumentException("Input must be an array of Integers or nested arrays of Integers");
- }
- }
- return (ArrayList<Integer>) flatList;
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement