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');
Answer from Andreas Dolk on 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.
java - Comparing and replacing values in an array - Software Engineering Stack Exchange
java replacing elements of an array - Stack Overflow
java - How to replace single element of array - Stack Overflow
How do I replace something in an array?
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.
First, I would give slow the same scope as fast - give them both loop scope:
for (int slow = 0, fast = 0; fast < nums.length; ++fast) {
Second, rather than creating a new variable i and iterating backwards, I would write my second loop like this:
for (; slow < nums.length; ++slow) {
nums[slow] = 0;
}
Instead of creating a new variable int i = fast and iterating backward until it is equal to slow to set the remaining elements of the array to 0, I just start with the variable in slow and iterate until the end of the array is reached. This will work because the outer loop has finished iterating anyway because once fast == nums.length - 1, we exit the outer loop.
Third, put spaces around your mathematical operator(s) to make it easy to read:
if (fast == nums.length - 1) {
Otherwise, I see nothing wrong with your code, you use reasonable variable names, you use braces around your ifs and loops, and you indent your code nicely.
It's extraordinarily confusing to have if (fast == nums.length - 1) as a conditional within a loop that continues while fast < nums.count returns true. This is the last part of the loop, and it only executes on the last iteration of the loop... so why is it even in the loop at all?
A couple of comments in our code can help us out. We have two distinct parts:
- Shuffling the non-tens to the front.
- Zero-ing out the tens.
So let's use comments to mark these in distinct blocks. And, let's move the second part to its own loop.
And while we're at it, let's make the method more generic to replace any number with another given number.
public int[] replaceNum(int[] nums, int eliminationNum, int replaceNum) {
int nextMoveIndex = 0;
// shuffle non-elims to the front
for (int i = 0; i < nums.length; ++i) {
if (nums[i] != eliminationNum) {
nums[nextMoveIndex++] = nums[i];
}
}
// zero out the elimination number
while (nextMoveIndex < nums.length) {
nums[nextMoveIndex++] = replaceNum;
}
return nums;
}