ArrayUtility Class Reference

Static Public Member Functions

static int[] addElement (int[] current, int elem)
 
static int[] deleteElement (int[] current, int index)
 
static int linearSearch (int[] current, int value)
 

Detailed Description

This class provides methods that duplicate the functionality of ArrayList but without the space and lookup overhead of double indirection. It only supports arrays of ints. Premature optimization? It's actually here because I didn't know 1301 covered ArrayList, so shut up.

Note that this class is wildly time-inefficient and unsuitable for use with anything but the smallest arrays. Unlike ArrayList, which uses exponential doubling of allocated arrays, these methods specify a system in which the array is reallocated at every addition and deletion. This is necessary because the length field has to always be exact so that students can rely on it.

Definition at line 12 of file ArrayUtility.java.

Member Function Documentation

static int [] ArrayUtility.addElement ( int[]  current,
int  elem 
)
static

Add element to array

Parameters
currentarray to duplicate and add element
elemelement to add
Returns
new array 1 element larger with elem appended

Definition at line 19 of file ArrayUtility.java.

20  {
21  int[] to_return = new int[current.length+1];
22  for(int i=0; i<current.length; i++)
23  to_return[i]=current[i];
24  to_return[to_return.length-1]=elem;
25  return to_return;
26  }
static int [] ArrayUtility.deleteElement ( int[]  current,
int  index 
)
static

Delete array element

Parameters
currentarray with element to delete
indexindex of element to delete
Returns
new array with element at index deleted

Definition at line 33 of file ArrayUtility.java.

34  {
35  int[] to_return = new int[current.length-1];
36  for(int i=0,j=0; i<current.length; i++,j++)
37  if(i==index)
38  j--;
39  else
40  to_return[j] = current[i];
41  return to_return;
42  }
static int ArrayUtility.linearSearch ( int[]  current,
int  value 
)
static

Linear search: Find first occurrence of value in array, return index

Parameters
currentarray
valuevalue to find
Returns
index if found, -1 if not

Definition at line 50 of file ArrayUtility.java.

51  {
52  for(int i=0; i<current.length; i++)
53  if(current[i]==value)
54  return i;
55  return -1;
56  }

The documentation for this class was generated from the following file: