Java Array Concatenate concat(T[] left, T[] right)

Here you can find the source of concat(T[] left, T[] right)

Description

concat

License

Open Source License

Declaration

public static <T> T[] concat(T[] left, T[] right) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import java.util.ArrayList;
import java.util.Arrays;

import java.util.Iterator;
import java.util.List;

public class Main {
    public static <T> T[] concat(T[] left, T[] right) {
        return list(chain(Arrays.asList(left), Arrays.asList(right))).toArray(left);
    }//from   w w  w.  java 2  s .  com

    public static <T> List<T> list(Iterable<T> input) {
        List<T> ret = new ArrayList<>();

        for (T item : input) {
            ret.add(item);
        }

        return ret;
    }

    public static <T> List<T> list(T[] input) {
        return Arrays.asList(input);
    }

    public static <T> Iterable<T> chain(final Iterable<? extends T> it1, final Iterable<? extends T> it2) {
        return new Iterable<T>() {
            @Override
            public Iterator<T> iterator() {
                class MyIt implements Iterator<T> {
                    private final Iterator<? extends T> i1;
                    private final Iterator<? extends T> i2;

                    public MyIt(Iterator<? extends T> i1, Iterator<? extends T> i2) {
                        this.i1 = i1;
                        this.i2 = i2;
                    }

                    @Override
                    public boolean hasNext() {
                        return i1.hasNext() || i2.hasNext();
                    }

                    @Override
                    public T next() {
                        return i1.hasNext() ? i1.next() : i2.next();
                    }

                    @Override
                    public void remove() {
                        throw new UnsupportedOperationException();
                    }
                }

                return new MyIt(it1.iterator(), it2.iterator());
            }
        };
    }
}

Related

  1. concat(T[] first, T[] second)
  2. concat(T[] first, T[] second)
  3. concat(T[] first, T[] second)
  4. concat(T[] first, T[]... rest)
  5. concat(T[] head, T... tail)
  6. concat(T[] objects)
  7. concat(T[]... arrays)
  8. concat(T[]... tss)
  9. concatAll(byte[] a, byte[]... rest)