java - Comparing and replacing values in an array - Software Engineering Stack Exchange
How do I replace something in an array?
Java: Replacing the values in an array and shifting the elements in an array - Stack Overflow
How do I replace an array element in Java? - Stack Overflow
String dna[] = {"ATCTA"};
int i = 0;
dna[i] = dna[i].replace('T', 'C');
System.out.println(dna[i]);
This works as expected. Double check your code if you follow a similiar pattern.
You may have expected, that dna[i].replace('T', 'C'); changes the content of the cell dna[i] directly. This is not the case, the String will not be changed, replace will return a new String where the char has been replaced. It's necessary to assign the result of the replace operation to a variable.
To answer your last comment:
Strings are immutable - you can't change a single char inside a String object. All operations on Strings (substring, replace, '+', ...) always create new Strings.
A way to make more than one replace is like this:
dna[i] = dna[i].replace('T', 'C').replace('A', 'S');
An array is just a data structure that holds data. It doesn't support any operations on that data. You need to write the algorithms to work on the data yourself.
A String is basically a char array with some methods that you can call on that. The replace() method is one of them.
The method you want would look something like this:
static void replace(char[] arr, char find, char replace) {
for (int i = 0; i < arr.length; i++) {
if (arr[i] == find) {
arr[i] = replace;
return;
}
}
}
You would then call it like so:
replace(dna, 'T', 'C');
That would replace the first instance of T in the array with a C.
I have an array with placeholder terms, call it double[] array = {0,1,2,3,4,5,6,7}. How do I write a statement that replaces one of the terms with a variable of the same type?
for example, replacing index 1 with a variable that is equal to 5.6.
for (pos=0;pos<array.length;pos++) {
if(array[pos]==null) {
array[pos]=xyz
}
}
do like this
String[] values= {null,"p", ";","z",null, "OR","y"};
List<String> list=new ArrayList<String>(Arrays.asList(values));
Collections.replaceAll(list, null, "newVal");
values = list.toArray(new String[list.size()]);
System.out.println(Arrays.toString(values));
The change you make is being overwritten by the draw method.
clearBoard[row1][col1]='.'; // this will print out the 4 row variable values
It's not clear to me what you're trying to accomplish in this method, since the comment completely disagrees with the code - you're setting the values to a period, not printing anything out at that line.
There's a lot of style things to improve here as well - a method named draw should not have the side effect of resetting the board.
In addition to @I82Much's answer, you declare the buildBoard method but never call it, so when you set clearBoard[0][0]=board[0][1] you are setting it to null (i think).
In your buildBoard method you are re-declaring a new board array that's local in scope.