Start line:  
End line:  

Snippet Preview

Snippet HTML Code

Stack Overflow Questions
  [The "BSD license"]
  Copyright (c) 2005-2009 Terence Parr
  All rights reserved.
  Redistribution and use in source and binary forms, with or without
  modification, are permitted provided that the following conditions
  are met:
  1. Redistributions of source code must retain the above copyright
     notice, this list of conditions and the following disclaimer.
 2. Redistributions in binary form must reproduce the above copyright
     notice, this list of conditions and the following disclaimer in the
     documentation and/or other materials provided with the distribution.
 3. The name of the author may not be used to endorse or promote products
     derived from this software without specific prior written permission.
package org.antlr.runtime.misc;

A dynamic array that uses int not Integer objects. In principle this is more efficient in time, but certainly in space. This is simple enough that you can access the data array directly, but make sure that you append elements only with add() so that you get dynamic sizing. Make sure to call ensureCapacity() when you are manually adding new elements. Doesn't impl List because it doesn't return objects and I mean this really as just an array not a List per se. Manipulate the elements at will. This has stack methods too. When runtime can be 1.5, I'll make this generic.
public class IntArray {
	public static final int INITIAL_SIZE = 10;
	public int[] data;
	protected int p = -1;
	public void add(int v) {
		[++] = v;
	public void push(int v) {
	public int pop() {
		int v = [];
		return v;

This only tracks elements added via push/add.
	public int size() {
		return ;
    public void clear() {
         = -1;
    public void ensureCapacity(int index) {
		if ( ==null ) {
			 = new int[];
		else if ( (index+1)>=. ) {
			int newSize = .*2;
			if ( index>newSize ) {
				newSize = index+1;
			int[] newData = new int[newSize];
			System.arraycopy(, 0, newData, 0, .);
			 = newData;
New to GrepCode? Check out our FAQ X