1

I several sets of arrays. The first array contains the ID of an item (in the example below, it would be the ID of a particular animal). The second array contains the QTY of that item.

public int[] animals;
public int[] animalsQTY

These are used in the following manner:

animals[0] = 123; // ID for pig, for e.g.
animalsQTY[0] = 4; // 4 pigs

I load values into these arrays from a MySQL database and have several sets of the data so I don't want to write the same loading code over and over again.

The problem is that if I do not load a value from the database into one of these fields, it must be a default value. In a language that allows passing variables by reference, I would send the variables to be updated to the loading method:

Sub LoadFromMySQL(ByVal TableName As String, ByRef UpdateA() As Integer, ByRef UpdateB() As Integer)

Then only change items in the provided arrays when a relating record is found in the MySQL database.

If I only had the one pair of arrays, I would do something similar to:

results = getMySQLresults();
foreach results as result
    animals[result['slot']] = result['id'];
    animalsQTY[result['slot']] = result['qty'];
end foreach

Yet I have lots of arrays to update. How can I turn the above pseudocode into a Java function / method?

3
  • 5
    I don't fully understand your question. But in an OO language like Java, instead of several arrays, each containing a different field of an animal (ID, quantity, etc.), you should have a single array or collection containing instances of an Animal class. And an Animal should have a property ID, a property quantity, etc. Commented Mar 11, 2012 at 19:01
  • You really need to pick up a basic Java book and read it, before venturing further with this project. Commented Mar 11, 2012 at 19:10
  • As far as I can understand , your problem needs some understanding of OOPS and use of Java Collection . Plz have a look on them .. u can frame your solution much better than u r doing now. Commented Mar 11, 2012 at 19:15

2 Answers 2

2

Java passes references by value so changes to objects referenced by them will be visible outside the method. If you already have your arrays defined, you can simply write:

    void loadFromMySql(String tableName, int[] arrayA, int[] arrayB) {
        ... code to fill the arrays ...
    }

That of course won't work if you want to create new arrays inside the method - in that case you have to create some wrapper object.

Also slighty offtopic: for your particular case it would better to either use Map (mapping id of the animal to the quantity) or array or list of

Sign up to request clarification or add additional context in comments.

Comments

1

Some basics on object orientation:

First create a class for your animals with fields for ID and Quantity.

public class MyCustomAnimal{
// field variables
    private int Id;
    private int Qty;

// getter and setter
    public int getId() {
        return this.Id;
    }
    public void setId(int id) {
        this.Id = id;
    }
    public int getQty() {
        return this.Qty;
    }
    public void setQty(int qty) {
        this.Qty = qty;
    }

// constructor
    public MyCustomAnimal(int id, int qty){
        this.Id = id;
        this.Qty = qty;
    }
}

Then create objects of the type MyCustomAnimal from your database query.

MyCustomAnimal animal = new MyCustomAnimal(123, 4);

Or even create an array of your animal objects.

MyCustomAnimal[] animal = new MyCustomAnimal[3];
animal[0] = new MyCustomAnimal(615, 7);
animal[1] = new MyCustomAnimal(654, 5);
animal[2] = new MyCustomAnimal(687, 9);

1 Comment

I understand that this would be the better approach yet am bound to using the format that is present in the script I'm working on. I'm trying to refactor it at the moment - hence using MySQL data loading as opposed to the previously used file handling. Converting these into objects is something that I intend to do a later date.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.