Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / Java

Growing Java Array Class

4.33/5 (2 votes)
30 May 2013CPOL 8.2K  
Growing Java array class.

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: 

C++
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package growingarrayclass;

import javax.swing.JOptionPane;

/**
 *
 * @author DJ
 */
public class GrowingArrayClass {

    /**
     * @param args the command line arguments
     */
    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++;
                }
            }
        }
        //asking the user if more strings will be added to the new array
        New = JOptionPane.showInputDialog("Do you want to add more to the array???");
    }
}
Java
/*Java Class*/

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package growingarrayclass;

/**
 *
 * @author DJ
 */
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;
        //growing temp array 
        tempS = new String[i];
        //copying ArrayName array into temp array.
        System.arraycopy(ArrayNameS, 0, tempS, 0, ArrayNameS.length);
        //creating a new array...which will reset the old array unfortunatly
        ArrayNameS = new String[i];
        //copying temp array into ArrayName
        System.arraycopy(tempS, 0, ArrayNameS, 0, tempS.length);   
        //subtracting allowing the user input to be stored correctly
        i = i - 1;
        //storing user input in the array
        ArrayNameS[i] = contentS;
        return ArrayNameS;
    }
    int[] ArrayIntBuilder (int i1, int[] ArrayNameI1, int contentI1)
    {
        i = i1;
        ArrayNameI = ArrayNameI1;
        contentI = contentI1;
        //growing temp array 
        tempI = new int[i];
        //copying ArrayName array into temp array.
        System.arraycopy(ArrayNameI, 0, tempI, 0, ArrayNameI.length);
        //creating a new array...which will reset the old array unfortunatly
        ArrayNameI = new int[i];
        //copying temp array into ArrayName
        System.arraycopy(tempI, 0, ArrayNameI, 0, tempI.length);   
        //subtracting allowing the user input to be stored correctly
        i = i - 1;
        //storing user input in the array
        ArrayNameI[i] = contentI;
        return ArrayNameI;
    }
    double[] ArrayDoubleBuilder (int i1, double[] ArrayNameD1, double contentD1)
    {
        i = i1;
        ArrayNameD = ArrayNameD1;
        contentD = contentD1;
        //growing temp array 
        tempD = new double[i];
        //copying ArrayName array into temp array.
        System.arraycopy(ArrayNameD, 0, tempD, 0, ArrayNameD.length);
        //creating a new array...which will reset the old array unfortunatly
        ArrayNameD = new double[i];
        //copying temp array into ArrayName
        System.arraycopy(tempD, 0, ArrayNameD, 0, tempD.length);   
        //subtracting allowing the user input to be stored correctly
        i = i - 1;
        //storing user input in the array
        ArrayNameD[i] = contentD;
        return ArrayNameD;
    }
    boolean[] ArrayBooleanBuilder (int i1, boolean[] ArrayNameB1, boolean contentB1)
    {
        i = i1;
        ArrayNameB = ArrayNameB1;
        contentB = contentB1;
        //growing temp array 
        tempB = new boolean[i];
        //copying ArrayName array into temp array.
        System.arraycopy(ArrayNameB, 0, tempB, 0, ArrayNameB.length);
        //creating a new array...which will reset the old array unfortunatly
        ArrayNameB = new boolean[i];
        //copying temp array into ArrayName
        System.arraycopy(tempB, 0, ArrayNameB, 0, tempB.length);   
        //subtracting allowing the user input to be stored correctly
        i = i - 1;
        //storing user input in the array
        ArrayNameB[i] = contentB;
        return ArrayNameB;
    }
}

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)