Jump to content

How do I update an array value (Java)?

- - - - -

This topic has been archived. This means that you cannot reply to this topic.
1 reply to this topic

#1
paulusyoungus

paulusyoungus

    Newbie

  • Members
  • Pip
  • 2 posts
I have the following array data, representing first the vertical then the horizontal axis then a value.

private static final String[] xy_data = {
"000", "010", "020", "030", "049", "050", "060", "070", "080",
"100", "110", "120", "135", "142", "150", "160", "178", "180",
"200", "210", "220", "231", "246", "253", "260", "270", "280",
"300", "313", "320", "330", "340", "350", "360", "370", "388",
"400", "410", "426", "439", "440", "457", "460", "470", "480",
"500", "510", "521", "530", "540", "550", "563", "577", "585",
"603", "618", "620", "630", "645", "650", "661", "670", "680",
"700", "710", "720", "734", "740", "752", "760", "770", "780",
"807", "812", "820", "833", "840", "851", "864", "870", "880"
};

I can then parse this array using:

static int[][] parseProblem(String[] xy_data) {
int[][] problem = new int[9][9];
for (int n = 0; n < xy_data.length; ++n) {
int i = Integer.parseInt(xy_data[n].substring(0, 1));
int j = Integer.parseInt(xy_data[n].substring(1, 2));
int val = Integer.parseInt(xy_data[n].substring(2, 3));
problem[i][j] = val;
}
return problem;
}

I then use this data to solve as Suduko puzzle using back tracing.

How do I set the new variable into the array (once calculated)? Do I need the row numer then simply setValue = newValue. This value would then need to passed to a jTextField on the GUI also preferably in the same method.

I previously posted this on yahoo answers but advised to look at:

Arrays (The Java™ Tutorials > Learning the Java Language > Language Basics)

Allthough this page explains arrays it doesnt advise how to update an existing array element value.

Thanks in advance. :)

#2
BuckAMayzing

BuckAMayzing

    Learning Programmer

  • Members
  • PipPipPip
  • 39 posts
It's just a matter of using the subscripts. Let's say you're changing column 3, row 6:

problem[3][6] = newValue;

Is that what you're looking for?