All Downloads are FREE. Search and download functionalities are using the official Maven repository.

edu.jhu.prim.list.ByteArrayList Maven / Gradle / Ivy

The newest version!
package edu.jhu.prim.list;

import java.io.Serializable;
import java.util.Arrays;

import edu.jhu.prim.Primitives;
import edu.jhu.prim.sort.ByteSort;

/**
 * Array list of byte primitives.
 * @author mgormley
 */
public class ByteArrayList implements Serializable {
    
    private static final long serialVersionUID = 1L;

    /** The internal array representing this list. */
    protected byte[] elements;
    /** The number of elements in the list. */
    protected int size;
    
    public ByteArrayList() {
        this(8);
    }

    public ByteArrayList(byte[] elements) {
        this.elements = elements;
        this.size = elements.length;
    }
    
    public ByteArrayList(int initialCapacity) {
        elements = new byte[initialCapacity];
        size = 0;
    }
    
    /** Copy constructor. */
    public ByteArrayList(ByteArrayList other) {
        this(other.size);
        add(other);
    }
    
    /**
     * Adds the value to the end of the list.
     * @param value The value to add.
     */
    public void add(byte value) {
        ensureCapacity(size + 1);
        elements[size] = value;
        size++;
    }
    
    /**
     * Gets the i'th element of the array list.
     * @param i The index of the element to get.
     * @return The value of the element to get.
     */
    public byte get(int i) {
        if (i < 0 || i >= size) {
            throw new IndexOutOfBoundsException("Index out of bounds: " + i);
        }
        return elements[i];
    }
    
    /**
     * Sets the i'th element of the array list to the given value.
     * @param i The index to set.
     * @param value The value to set.
     */
    public void set(int i, byte value) {
        if (i < 0 || i >= size) {
            throw new IndexOutOfBoundsException();
        }
        elements[i] = value;
    }

    /**
     * Adds all the elements in the given array to the array list.
     * @param values The values to add to the array list.
     */
    public void add(byte[] values) {
        ensureCapacity(size + values.length);
        for (byte element : values) {
            this.add(element);
        }
    }
    
    /**
     * Adds all the elements in the given array list to the array list.
     * @param values The values to add to the array list.
     */
    public void add(ByteArrayList values) {
        ensureCapacity(size + values.size);
        for (int i=0; i elements.length) {
            byte[] tmp = new byte[size*2];
            System.arraycopy(elements, 0, tmp, 0, elements.length);
            elements = tmp;
        }
        return elements;
    }

    /**
     * Gets the number of elements in the list.
     * @return The size of the list.
     */
    public int size() {
        return size;
    }
    
    /** Returns true iff the list is empty. */
    public boolean isEmpty() {
        return size == 0;
    }
    
    /**
     * Removes all elements from this array list.
     */
    public void clear() {
        size = 0;
    }
    
    @Override
    public String toString() {
        StringBuilder sb = new StringBuilder();
        sb.append("ByteArrayList [");
        for (int i=0; i




© 2015 - 2024 Weber Informatics LLC | Privacy Policy