Mivox
Mivox

Reputation: 3

Using a string to write to an array

Attempting to tidy up code, originally I was using this method of writing to arrays, which is ridiculously long when I have to repeat it 20 times

        if (ant.getAntNumber() == 3)
        {
            numbers3.add(ant.getCol());
            numbers3y.add(ant.getRow());
        }

        if (ant.getAntNumber() == 4)
        {
            numbers4.add(ant.getCol());
            numbers4y.add(ant.getRow());
        }

I attempted to use a for loop to do it but I cant figure out how to add to the array using the string value, because it thinks its a string rather than trying to use the array

for (int j = 0; j<maxAnts; j++)
        {
            String str = "numbers" + j;
            String str2 = "numbers" + j + "y";
            //this part doesnt work
            str.add(ant.getCol());

        }

Any suggestions would be helpful

Upvotes: 0

Views: 73

Answers (3)

Dani&#235;l Knippers
Dani&#235;l Knippers

Reputation: 3055

A straight-forward port of your code would be to use two Map<Integer, Integer> which store X and Y coordinates. From your code it seems like ant numbers are unique, i.e., we only have to store a single X and Y value per ant number. If you need to store multiple values per ant number, use a List<Integer> as value type of the Map instead.

Map<Integer, Integer> numbersX = new HashMap<Integer, Integer>();
Map<Integer, Integer> numbersY = new HashMap<Integer, Integer>();

for(Ant ant : ants) {
    int number = ant.getAntNumber();        
    numbersX.put(number, ant.getCol());
    numbersY.put(number, ant.getRow());
}

Upvotes: 1

aliteralmind
aliteralmind

Reputation: 20163

How about this?

Ant[] aAnt = new Ant[20];

//Fill the ant-array

int[] aColumns = new int[aAnt.length];
int[] aRows = new int[aAnt.length];

for(int i = 0; i < aAnt.length; i++)  {
   aColumns[i] = aAnt[i].getCol();
   aRows[i] = aAnt[i].getRow();
}

or with lists:

List<Integer> columnList = new List<Integer>(aAnt.length);
List<Integer> rowList = new List<Integer>(aAnt.length);

for(Ant ant : aAnt)  {
   columnList.add(ant.getCol());
   rowList.add(ant.getRow());
}

or with a col/row object:

class Coordinate  {
   public final int yCol;
   public final int xRow;
   public Coordinate(int y_col, int x_row)  {
      yCol = y_col;
      xRow = x_row;
   }
}

//use it with

List<Coordinate> coordinateList = new List<Coordinate>(aAnt.length);
for(Ant ant : aAnt)  {
   coordinateList.add(ant.getCol(), ant.getRow());
}

Upvotes: 1

rgettman
rgettman

Reputation: 178263

In Java, you cannot use the value of a String object to reference an actual variable name. Java will think you're attempting to to call add on the String object, which doesn't exist and gives you the compiler error you're seeing.

To avoid the repetition, you need to add your Lists to two master lists that you can index.

In your question, you mention arrays, but you call add, so I'm assuming that you're really referring to Lists of some sort.

List<List<Integer>> numbers = new ArrayList<List<Integer>>(20);
List<List<Integer>> numbersy = new ArrayList<List<Integer>>(20);
// Add 20 ArrayList<Integer>s to each of the above lists in a loop here.

Then you can bounds-check ant.getAntNumber() and use it as an index into your master lists.

int antNumber = ant.getAntNumber();
// Make sure it's within range here.

numbers.get(antNumber).add(ant.getCol());
numbersy.get(antNumber).add(ant.getRow());

Upvotes: 1

Related Questions