Best Way to Store a 2D Array within a Program

Viewed 68

Alright, so I am starting a small object oriented program, but I still need help to wrap my head around the model classes.

I have 5 class files: UFile.java, Menu.java, UInput.java, UImport.java and UMath.java

And a model class: Matrix.java

Each class file is pretty explanatory

  • UFile.java: Handles file input from .csv and writes to .csv

    Menu.java provides the user with prompts to select what they would like to do

    UInput.java Handles input from user and validates it

    UMath.java Handles any math related algorithms

    UImport.java Imports the array to the program

I have tested each file with its own test file, and they work fine

The big question I have is how do I store my Matrix array into the program successfully, everything is dynamic so there is no hard written code, I need to be able to import it with my UFile.java and then use my UMath.java to perform multiplication on it, while also having to validate it to make sure no single element is null or invalid

I apologize if this is a stupid question but I have searched a lot and cannot find a definitive answer on it

For example: If I used my menu.java file to import a matrix this is what it would look like

Menu.java:

    public static void main(String[] args){

    Scanner s = new Scanner(System.in);
    int selection;

    do{
        System.out.println("Please select\n" +
                        "1 Import Matrix\n" +
                        "2 Multiply Matrix\n" +
                        "3: Exit\n");
        selection = s.nextInt();

        switch(selection)
        {
            case 1: 
                importMatrix();
                break;
            case 2:
                multiplyMatrix();
                break;
            case 3:
                System.out.println("Goodbye!");
                break;
            default:
                System.out.println("Invalid Input");
        }
    } while(selection != 3);
}

And then the importMatrix static class, which just leads to the import class

private static void importMatrix(){

    UImport.fileIn();

Where I start to have trouble on how to store the matrix, again I have been able to store a hardcoded array as a global variable but I am not sure how I could store it as it should be dynamic.

This is what I have now, which wont work as I need to be able to use and edit the array through out the program.

private static void fileIn() {
    String file;

    file = UInput.userIn("Please enter the file name");

    int[][] matrixA = UFile.readFile(file);

My class still needs to be completed:

public class Matrix{

private int [][] MatrixA
private int [][] MatrixB


public Matrix(){


}


public Matrix(int [][] MatrixA, int [][] MatrixB){
    setMatrixA(inMatrixA);
    setMatrixB(inMatrixB);
}

public Matrix(Matrix inMatrix){
    MatrixA = inMatrix.getMatrixA();
    MatrixB = inMatrix.getMatrixA();
}

public void setMatrixA(String inMatrixA){
    if(validateMatrixA(inMatrixA))
    {
        MatrixA = inMatrixA;
    }
    else
    {
        throw new IllegalArgumentException("Invalid Matrix");
    }
}

public void setMatrixB(int inMatrixB){
    if(validateMatrixB(inMatrixB))
    {
        MatrixB = inMatrixB;
    }
    else
    {
        throw new IllegalArgumentException("Invalid Matrix");
    }
}


//ACCESSORS
public int [][] MatrixA(){
    return MatrixA;
}

public int [][] getMatrixB(){
    return MatrixB;
}

public boolean equals(Object inObj){
    boolean same = false;
    if(inObj instanceof Matrix)
    {
        Matrix inMatrix = (Matrix)inObj;
        same = MatrixA.equals(inMatrix.getMatrixA()) && 
               MatrixB.equals(inMatrix.getMatrixB());
    }
    return same;
}

public Matrix clone()
{
    return new Matrix(this);
}

public String toString()
{
    return ();
}

public String toFileString()
{

}
}

If I have missed out anything important please let me know, I am a beginner so I am not sure

1 Answers

A fixed-sized array is fine if the number of elements to be stored is equal to the size of the array. However, if it is not the case (i.e. if the number of elements to be stored can be less than or more than the specified size), you should use a Collection e.g. an ArrayList which is a kind of dynamic array (i.e. grows automatically). This will help you in many ways:

  1. You will save memory if the number of elements to be stored is less than the specified size.
  2. You do not need to change your code in order to increase the size of the array when you need to store more elements than you have already specified.
  3. The Collection provides a rich API to deal with elements. By leveraging this API, your code can become compact, more performant, maintainable etc.
Related