Introduction
I developed this class to allow programmers to better use Java arrays. Java's arrays are developed to have a pre-set size. My class will allow programmers to no longer anticipate the size needed for an array; instead, the array will grow through the course of the program.
Using the code
Java main:
package growingarrayclass;
import javax.swing.JOptionPane;
public class GrowingArrayClass {
public static void main(String[] args) {
int i = 0;
int j = 0;
int k = 0;
int l = 0;
int m = 0;
int n = 0;
int contentI = 0;
double contentD = 0.0;
boolean contentB = false;
String contentS = "";
int[] ArrayNameI = new int[i];
double[] ArrayNameD = new double[i];
String[] ArrayNameS = new String[i];
boolean[] ArrayNameB = new boolean[i];
String New = "";
String ArrayType = "";
String NewLine = "";
ArrayGrow Array = new ArrayGrow(i, n, ArrayNameS, contentS,
ArrayNameI, contentI, ArrayNameD, contentD, ArrayNameB,contentB);
while (!New.equalsIgnoreCase("no"))
{
ArrayType = JOptionPane.showInputDialog(
"What type of array do you want??? (string)," +
" (int), (double), (boolean), or (StringS)?");
if (ArrayType.equalsIgnoreCase("string"))
{
contentS = JOptionPane.showInputDialog("Type something for the array");
i++;
ArrayNameS = Array.ArrayStringBuilder(i, ArrayNameS, contentS);
System.out.println("");
System.out.println("New ''String'' Array");
l = 0;
while (l != ArrayNameS.length)
{
System.out.println("Array contains"+" "+
"["+l+"]"+" "+ArrayNameS[l]);
l++;
}
}
else if (ArrayType.equalsIgnoreCase("int"))
{
contentS = JOptionPane.showInputDialog("Type a number for the array (ex: 1)");
contentI = Integer.parseInt(contentS);
j++;
ArrayNameI = Array.ArrayIntBuilder(j, ArrayNameI, contentI);
System.out.println("");
System.out.println("New ''int'' Array");
l = 0;
while (l != ArrayNameI.length)
{
System.out.println("This Array contains"+" "+
"["+l+"]"+" "+ArrayNameI[l]);
l++;
}
}
else if (ArrayType.equalsIgnoreCase("double"))
{
contentS = JOptionPane.showInputDialog("Type a number for the array (ex: 1.2)");
contentD = Double.parseDouble(contentS);
k++;
ArrayNameD = Array.ArrayDoubleBuilder(k, ArrayNameD, contentD);
System.out.println("");
System.out.println("New ''double'' Array");
l = 0;
while (l != ArrayNameD.length)
{
System.out.println("This Array contains"+" "+
"["+l+"]"+" "+ArrayNameD[l]);
l++;
}
}
else if (ArrayType.equalsIgnoreCase("boolean"))
{
if (m == 0)
{
contentB = true;
m++;
}
else if (m == 1)
{
contentB = false;
m = 0;
}
k++;
ArrayNameB = Array.ArrayBooleanBuilder(k, ArrayNameB, contentB);
System.out.println("");
System.out.println("New ''Boolean'' Array");
l = 0;
while (l != ArrayNameB.length)
{
System.out.println("This Array contains"+" "+
"["+l+"]"+" "+ArrayNameB[l]);
l++;
}
}
}
New = JOptionPane.showInputDialog("Do you want to add more to the array???");
}
}
package growingarrayclass;
public class ArrayGrow {
int i;
int j;
String[] ArrayNameS;
String[] tempS;
String contentS;
int[] ArrayNameI;
int[] tempI;
int contentI;
double[] ArrayNameD;
double[] tempD;
double contentD;
boolean [] ArrayNameB;
boolean [] tempB;
boolean contentB;
ArrayGrow()
{
i = 0;
j = 0;
ArrayNameS = new String[i];
tempS = new String[i];
contentS = "";
ArrayNameI = new int[i];
tempI = new int[i];
contentI = 0;
ArrayNameD = new double[i];
tempD = new double[i];
contentD = 0.0;
ArrayNameB = new boolean [i];
tempB = new boolean [i];
contentB = false;
}
ArrayGrow(int i1, int j1, String[] ArrayNameS1, String contentS1,
int[] ArrayNameI1, int contentI1, double[] ArrayNameD1,
double contentD1, boolean [] ArrayNameB1, boolean contentB1)
{
i = i1;
j = j1;
ArrayNameS = ArrayNameS1;
contentS = contentS1;
ArrayNameI = ArrayNameI1;
contentI = contentI1;
ArrayNameD = ArrayNameD1;
contentD = contentD1;
ArrayNameB = ArrayNameB1;
contentB = contentB1;
}
String[] ArrayStringBuilder (int i1, String[] ArrayNameS1, String contentS1)
{
i = i1;
ArrayNameS = ArrayNameS1;
contentS = contentS1;
tempS = new String[i];
System.arraycopy(ArrayNameS, 0, tempS, 0, ArrayNameS.length);
ArrayNameS = new String[i];
System.arraycopy(tempS, 0, ArrayNameS, 0, tempS.length);
i = i - 1;
ArrayNameS[i] = contentS;
return ArrayNameS;
}
int[] ArrayIntBuilder (int i1, int[] ArrayNameI1, int contentI1)
{
i = i1;
ArrayNameI = ArrayNameI1;
contentI = contentI1;
tempI = new int[i];
System.arraycopy(ArrayNameI, 0, tempI, 0, ArrayNameI.length);
ArrayNameI = new int[i];
System.arraycopy(tempI, 0, ArrayNameI, 0, tempI.length);
i = i - 1;
ArrayNameI[i] = contentI;
return ArrayNameI;
}
double[] ArrayDoubleBuilder (int i1, double[] ArrayNameD1, double contentD1)
{
i = i1;
ArrayNameD = ArrayNameD1;
contentD = contentD1;
tempD = new double[i];
System.arraycopy(ArrayNameD, 0, tempD, 0, ArrayNameD.length);
ArrayNameD = new double[i];
System.arraycopy(tempD, 0, ArrayNameD, 0, tempD.length);
i = i - 1;
ArrayNameD[i] = contentD;
return ArrayNameD;
}
boolean[] ArrayBooleanBuilder (int i1, boolean[] ArrayNameB1, boolean contentB1)
{
i = i1;
ArrayNameB = ArrayNameB1;
contentB = contentB1;
tempB = new boolean[i];
System.arraycopy(ArrayNameB, 0, tempB, 0, ArrayNameB.length);
ArrayNameB = new boolean[i];
System.arraycopy(tempB, 0, ArrayNameB, 0, tempB.length);
i = i - 1;
ArrayNameB[i] = contentB;
return ArrayNameB;
}
}