Create an Int Vector in Java
Description
The following code shows how to create an Int Vector.
Example
//w w w .j a v a2 s. c om
import java.io.Serializable;
public class IntVector implements Serializable {
/**
*
*/
private static final long serialVersionUID = 1L;
public final static int DEFAULT_INCREMENT = 256;
private int _increment;
private int _data[];
private int _size = 0;
public IntVector() {
_increment = DEFAULT_INCREMENT;
_data = new int[_increment];
}
public IntVector(int increment) {
_increment = increment;
_data = new int[increment];
}
public IntVector(int initialSize, int increment) {
_increment = increment;
_data = new int[initialSize];
_size = initialSize;
}
public IntVector(int[] data) {
_data = new int[data.length];
_size = data.length;
_increment = DEFAULT_INCREMENT;
System.arraycopy(data, 0, _data, 0, _size);
}
/**
* Get the length of the list.
*
* @return length of the list
*/
public final int size() {
return _size;
}
/**
* Get the length of the list.
*
* @return length of the list
*/
public final void setSize(int sz) {
_size = sz;
}
/**
* Append a int onto the vector.
*
* @param value
* Int to add to the list
*/
public final void addElement(int value) {
ensureCapacity(_size+1);
_data[_size] = value;
_size++;
}
private void ensureCapacity(int size){
if (size >= _data.length) {
int newData[] = new int[size + _increment];
System.arraycopy(_data, 0, newData, 0, _data.length);
_data = newData;
}
}
/**
* Append several int values onto the vector.
*
* @param value
* Int to add to the list
*/
public final void addElements(int value, int numberOfElements) {
ensureCapacity(_size+numberOfElements);
for (int i = 0; i < numberOfElements; i++) {
_data[_size] = value;
_size++;
}
}
/**
* Inserts the specified node in this vector at the specified index.
*
* @param value
* Int to insert
* @param at
* Index of where to insert
*/
public final void insertElementAt(int value, int at) {
ensureCapacity(_size+1);
if (at <= (_size - 1)) {
System.arraycopy(_data, at, _data, at + 1, _size - at);
}
_data[at] = value;
_size++;
}
public final void removeAllElements() {
for (int i = 0; i < _size; i++) {
_data[i] = java.lang.Integer.MIN_VALUE;
}
_size = 0;
}
/**
* Removes the first occurrence of the argument from this vector. If the
* object is found in this vector, each component in the vector with an
* index greater or equal to the object's index is shifted downward to have
* an index one smaller than the value it had previously.
*
* @param s
* Int to remove from array
*
* @return True if the int was removed, false if it was not found
*/
public final boolean removeElement(int s) {
for (int i = 0; i < _size; i++) {
if (_data[i] == s) {
if ((i + 1) < _size)
System.arraycopy(_data, i + 1, _data, i - 1, _size - i);
else
_data[i] = java.lang.Integer.MIN_VALUE;
_size--;
return true;
}
}
return false;
}
/**
* Deletes the component at the specified index. Each component in this
* vector with an index greater or equal to the specified index is shifted
* downward to have an index one smaller than the value it had previously.
*
* @param i
* index of where to remove and int
*/
public final void removeElementAt(int i) {
if (i > _size)
System.arraycopy(_data, i + 1, _data, i, _size);
else
_data[i] = java.lang.Integer.MIN_VALUE;
_size--;
}
/**
* Sets the component at the specified index of this vector to be the
* specified object. The previous component at that position is discarded.
*
* The index must be a value greater than or equal to 0 and less than the
* current size of the vector.
*
* @param node
* object to set
* @param index
* Index of where to set the object
*/
public final void setElementAt(int value, int index) {
_data[index] = value;
}
/**
* Get the nth element.
*
* @param i
* index of object to get
*
* @return object at given index
*/
public final int elementAt(int i) {
return _data[i];
}
/**
* Tell if the table contains the given node.
*
* @param s
* object to look for
*
* @return true if the object is in the list
*/
public final boolean contains(int s) {
for (int i = 0; i < _size; i++) {
if (_data[i] == s) return true;
}
return false;
}
/**
* Searches for the first occurence of the given argument, beginning the
* search at index, and testing for equality using the equals method.
*
* @param elem
* object to look for
* @param index
* Index of where to begin search
* @return the index of the first occurrence of the object argument in this
* vector at position index or later in the vector; returns -1 if
* the object is not found.
*/
public final int indexOf(int elem, int index) {
for (int i = index; i < _size; i++) {
if (_data[i] == elem) return i;
}
return java.lang.Integer.MIN_VALUE;
}
/**
* Searches for the first occurence of the given argument, beginning the
* search at index, and testing for equality using the equals method.
*
* @param elem
* object to look for
* @return the index of the first occurrence of the object argument in this
* vector at position index or later in the vector; returns -1 if
* the object is not found.
*/
public final int indexOf(int elem) {
for (int i = 0; i < _size; i++) {
if (_data[i] == elem) return i;
}
return java.lang.Integer.MIN_VALUE;
}
/**
* Searches for the first occurence of the given argument, beginning the
* search at index, and testing for equality using the equals method.
*
* @param elem
* Object to look for
* @return the index of the first occurrence of the object argument in this
* vector at position index or later in the vector; returns -1 if
* the object is not found.
*/
public final int lastIndexOf(int elem) {
for (int i = (_size - 1); i >= 0; i--) {
if (_data[i] == elem) return i;
}
return java.lang.Integer.MIN_VALUE;
}
public int[] getDataAsArray() {
return _data;
}
public final int binarySearch(int key){
int low = 0;
int high = _size;
while (low <= high) {
int mid = (low + high) >> 1;
int midVal = _data[mid];
if (midVal < key)
low = mid + 1;
else if (midVal > key)
high = mid - 1;
else
return mid; // key found
}
return -(low + 1); // key not found.
}
}
/*
Copyright (C) 2007 Mobixess Inc. http://www.java-objects-database.com
This file is part of the JODB (Java Objects Database) open source project.
JODB is free software; you can redistribute it and/or modify it under
the terms of version 2 of the GNU General Public License as published
by the Free Software Foundation.
JODB is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or
FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
for more details.
You should have received a copy of the GNU General Public License along
with this program; if not, write to the Free Software Foundation, Inc.,
59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
Create an Int Array List
// Copyright (c) 2003-2009, Jodd Team (jodd.org). All Rights Reserved.
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.IOException;
/**
* ArrayList of int primitives.
*/
public class IntArrayList implements Serializable {
private int[] array;
private int size;
public static int initialCapacity = 10;
/**
* Constructs an empty list with an initial capacity.
*/
public IntArrayList() {
this(initialCapacity);
}
/**
* Constructs an empty list with the specified initial capacity.
*/
public IntArrayList(int initialCapacity) {
if (initialCapacity < 0) {
throw new IllegalArgumentException("Capacity can't be negative: " + initialCapacity);
}
array = new int[initialCapacity];
size = 0;
}
/**
* Constructs a list containing the elements of the specified array.
* The list instance has an initial capacity of 110% the size of the specified array.
*/
public IntArrayList(int[] data) {
array = new int[(int) (data.length * 1.1) + 1];
size = data.length;
System.arraycopy(data, 0, array, 0, size);
}
// ---------------------------------------------------------------- conversion
/**
* Returns an array containing all of the elements in this list in the correct order.
*/
public int[] toArray() {
int[] result = new int[size];
System.arraycopy(array, 0, result, 0, size);
return result;
}
// ---------------------------------------------------------------- methods
/**
* Returns the element at the specified position in this list.
*/
public int get(int index) {
checkRange(index);
return array[index];
}
/**
* Returns the number of elements in this list.
*/
public int size() {
return size;
}
/**
* Removes the element at the specified position in this list.
* Shifts any subsequent elements to the left (subtracts
* one from their indices).
*
* @param index the index of the element to remove
* @return the value of the element that was removed
* @throws UnsupportedOperationException when this operation is not
* supported
* @throws IndexOutOfBoundsException if the specified index is out of range
*/
public int remove(int index) {
checkRange(index);
int oldval = array[index];
int numtomove = size - index - 1;
if (numtomove > 0) {
System.arraycopy(array, index + 1, array, index, numtomove);
}
size--;
return oldval;
}
/**
* Removes from this list all of the elements whose index is between fromIndex,
* inclusive and toIndex, exclusive. Shifts any succeeding elements to the left (reduces their index).
*/
public void removeRange(int fromIndex, int toIndex) {
checkRange(fromIndex);
checkRange(toIndex);
if (fromIndex >= toIndex) {
return;
}
int numtomove = size - toIndex;
if (numtomove > 0) {
System.arraycopy(array, toIndex, array, fromIndex, numtomove);
}
size -= (toIndex - fromIndex);
}
/**
* Replaces the element at the specified position in this list with the specified element.
*
* @param index the index of the element to change
* @param element the value to be stored at the specified position
* @return the value previously stored at the specified position
*/
public int set(int index, int element) {
checkRange(index);
int oldval = array[index];
array[index] = element;
return oldval;
}
/**
* Appends the specified element to the end of this list.
*/
public void add(int element) {
ensureCapacity(size + 1);
array[size++] = element;
}
/**
* Inserts the specified element at the specified position in this list.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
* @param index the index at which to insert the element
* @param element the value to insert
*/
public void add(int index, int element) {
checkRangeIncludingEndpoint(index);
ensureCapacity(size + 1);
int numtomove = size - index;
System.arraycopy(array, index, array, index + 1, numtomove);
array[index] = element;
size++;
}
/**
* Appends all of the elements in the specified array to the end of this list.
*/
public void addAll(int[] data) {
int dataLen = data.length;
if (dataLen == 0) {
return;
}
int newcap = size + (int) (dataLen * 1.1) + 1;
ensureCapacity(newcap);
System.arraycopy(data, 0, array, size, dataLen);
size += dataLen;
}
/**
* Appends all of the elements in the specified array at the specified position in this list.
*/
public void addAll(int index, int[] data) {
int dataLen = data.length;
if (dataLen == 0) {
return;
}
int newcap = size + (int) (dataLen * 1.1) + 1;
ensureCapacity(newcap);
System.arraycopy(array, index, array, index + dataLen, size - index);
System.arraycopy(data, 0, array, index, dataLen);
size += dataLen;
}
/**
* Removes all of the elements from this list.
* The list will be empty after this call returns.
*/
public void clear() {
size = 0;
}
// ---------------------------------------------------------------- search
/**
* Returns true if this list contains the specified element.
*/
public boolean contains(int data) {
for (int i = 0; i < size; i++) {
if (array[i] == data) {
return true;
}
}
return false;
}
/**
* Searches for the first occurence of the given argument.
*/
public int indexOf(int data) {
for (int i = 0; i < size; i++) {
if (array[i] == data) {
return i;
}
}
return -1;
}
/**
* Returns the index of the last occurrence of the specified object in this list.
*/
public int lastIndexOf(int data) {
for (int i = size - 1; i >= 0; i--) {
if (array[i] == data) {
return i;
}
}
return -1;
}
/**
* Tests if this list has no elements.
*/
public boolean isEmpty() {
return size == 0;
}
// ---------------------------------------------------------------- capacity
/**
* Increases the capacity of this ArrayList instance, if necessary,
* to ensure that it can hold at least the number of elements specified by
* the minimum capacity argument.
*/
public void ensureCapacity(int mincap) {
if (mincap > array.length) {
int newcap = ((array.length * 3) >> 1) + 1;
int[] olddata = array;
array = new int[newcap < mincap ? mincap : newcap];
System.arraycopy(olddata, 0, array, 0, size);
}
}
/**
* Trims the capacity of this instance to be the list's current size.
* An application can use this operation to minimize the storage of some instance.
*/
public void trimToSize() {
if (size < array.length) {
int[] olddata = array;
array = new int[size];
System.arraycopy(olddata, 0, array, 0, size);
}
}
// ---------------------------------------------------------------- serializable
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeInt(array.length);
for (int i = 0; i < size; i++) {
out.writeInt(array[i]);
}
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
array = new int[in.readInt()];
for (int i = 0; i < size; i++) {
array[i] = in.readInt();
}
}
// ---------------------------------------------------------------- privates
private void checkRange(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index should be at least 0 and less than " + size + ", found " + index);
}
}
private void checkRangeIncludingEndpoint(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Index should be at least 0 and at most " + size + ", found " + index);
}
}
}
Create a Long Array List
// Copyright (c) 2003-2009, Jodd Team (jodd.org). All Rights Reserved.
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.Serializable;
import java.io.IOException;
/**
* ArrayList of long primitives.
*/
public class LongArrayList implements Serializable {
private long[] array;
private int size;
public static int initialCapacity = 10;
/**
* Constructs an empty list with an initial capacity.
*/
public LongArrayList() {
this(initialCapacity);
}
/**
* Constructs an empty list with the specified initial capacity.
*/
public LongArrayList(int initialCapacity) {
if (initialCapacity < 0) {
throw new IllegalArgumentException("Capacity can't be negative: " + initialCapacity);
}
array = new long[initialCapacity];
size = 0;
}
/**
* Constructs a list containing the elements of the specified array.
* The list instance has an initial capacity of 110% the size of the specified array.
*/
public LongArrayList(long[] data) {
array = new long[(int) (data.length * 1.1) + 1];
size = data.length;
System.arraycopy(data, 0, array, 0, size);
}
// ---------------------------------------------------------------- conversion
/**
* Returns an array containing all of the elements in this list in the correct order.
*/
public long[] toArray() {
long[] result = new long[size];
System.arraycopy(array, 0, result, 0, size);
return result;
}
// ---------------------------------------------------------------- methods
/**
* Returns the element at the specified position in this list.
*/
public long get(int index) {
checkRange(index);
return array[index];
}
/**
* Returns the number of elements in this list.
*/
public int size() {
return size;
}
/**
* Removes the element at the specified position in this list.
* Shifts any subsequent elements to the left (subtracts
* one from their indices).
*
* @param index the index of the element to remove
* @return the value of the element that was removed
* @throws UnsupportedOperationException when this operation is not
* supported
* @throws IndexOutOfBoundsException if the specified index is out of range
*/
public long remove(int index) {
checkRange(index);
long oldval = array[index];
int numtomove = size - index - 1;
if (numtomove > 0) {
System.arraycopy(array, index + 1, array, index, numtomove);
}
size--;
return oldval;
}
/**
* Removes from this list all of the elements whose index is between fromIndex,
* inclusive and toIndex, exclusive. Shifts any succeeding elements to the left (reduces their index).
*/
public void removeRange(int fromIndex, int toIndex) {
checkRange(fromIndex);
checkRange(toIndex);
if (fromIndex >= toIndex) {
return;
}
int numtomove = size - toIndex;
if (numtomove > 0) {
System.arraycopy(array, toIndex, array, fromIndex, numtomove);
}
size -= (toIndex - fromIndex);
}
/**
* Replaces the element at the specified position in this list with the specified element.
*
* @param index the index of the element to change
* @param element the value to be stored at the specified position
* @return the value previously stored at the specified position
*/
public long set(int index, long element) {
checkRange(index);
long oldval = array[index];
array[index] = element;
return oldval;
}
/**
* Appends the specified element to the end of this list.
*/
public void add(long element) {
ensureCapacity(size + 1);
array[size++] = element;
}
/**
* Inserts the specified element at the specified position in this list.
* Shifts the element currently at that position (if any) and any subsequent
* elements to the right (adds one to their indices).
*
* @param index the index at which to insert the element
* @param element the value to insert
*/
public void add(int index, long element) {
checkRangeIncludingEndpoint(index);
ensureCapacity(size + 1);
int numtomove = size - index;
System.arraycopy(array, index, array, index + 1, numtomove);
array[index] = element;
size++;
}
/**
* Appends all of the elements in the specified array to the end of this list.
*/
public void addAll(long[] data) {
int dataLen = data.length;
if (dataLen == 0) {
return;
}
int newcap = size + (int) (dataLen * 1.1) + 1;
ensureCapacity(newcap);
System.arraycopy(data, 0, array, size, dataLen);
size += dataLen;
}
/**
* Appends all of the elements in the specified array at the specified position in this list.
*/
public void addAll(int index, long[] data) {
int dataLen = data.length;
if (dataLen == 0) {
return;
}
int newcap = size + (int) (dataLen * 1.1) + 1;
ensureCapacity(newcap);
System.arraycopy(array, index, array, index + dataLen, size - index);
System.arraycopy(data, 0, array, index, dataLen);
size += dataLen;
}
/**
* Removes all of the elements from this list.
* The list will be empty after this call returns.
*/
public void clear() {
size = 0;
}
// ---------------------------------------------------------------- search
/**
* Returns true if this list contains the specified element.
*/
public boolean contains(long data) {
for (int i = 0; i < size; i++) {
if (array[i] == data) {
return true;
}
}
return false;
}
/**
* Searches for the first occurence of the given argument.
*/
public int indexOf(long data) {
for (int i = 0; i < size; i++) {
if (array[i] == data) {
return i;
}
}
return -1;
}
/**
* Returns the index of the last occurrence of the specified object in this list.
*/
public int lastIndexOf(long data) {
for (int i = size - 1; i >= 0; i--) {
if (array[i] == data) {
return i;
}
}
return -1;
}
/**
* Tests if this list has no elements.
*/
public boolean isEmpty() {
return size == 0;
}
// ---------------------------------------------------------------- capacity
/**
* Increases the capacity of this ArrayList instance, if necessary,
* to ensure that it can hold at least the number of elements specified by
* the minimum capacity argument.
*/
public void ensureCapacity(int mincap) {
if (mincap > array.length) {
int newcap = ((array.length * 3) >> 1) + 1;
long[] olddata = array;
array = new long[newcap < mincap ? mincap : newcap];
System.arraycopy(olddata, 0, array, 0, size);
}
}
/**
* Trims the capacity of this instance to be the list's current size.
* An application can use this operation to minimize the storage of some instance.
*/
public void trimToSize() {
if (size < array.length) {
long[] olddata = array;
array = new long[size];
System.arraycopy(olddata, 0, array, 0, size);
}
}
// ---------------------------------------------------------------- serializable
private void writeObject(ObjectOutputStream out) throws IOException {
out.defaultWriteObject();
out.writeInt(array.length);
for (int i = 0; i < size; i++) {
out.writeLong(array[i]);
}
}
private void readObject(ObjectInputStream in) throws IOException, ClassNotFoundException {
in.defaultReadObject();
array = new long[in.readInt()];
for (int i = 0; i < size; i++) {
array[i] = in.readLong();
}
}
// ---------------------------------------------------------------- privates
private void checkRange(int index) {
if (index < 0 || index >= size) {
throw new IndexOutOfBoundsException("Index should be at least 0 and less than " + size + ", found " + index);
}
}
private void checkRangeIncludingEndpoint(int index) {
if (index < 0 || index > size) {
throw new IndexOutOfBoundsException("Index should be at least 0 and at most " + size + ", found " + index);
}
}
}
Home »
Java Tutorial »
Java Data Type »
Java Tutorial »
Java Data Type »