What I trying to do is creating an empty array such as
char[] array = {};
then I have a method called append
static void append(char array[], char x) {
array = Arrays.copyOf(array, array.length + 1);
array[array.length - 1] = x;
}
for example, I try to append x into the empty array and it keeps throwing Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: Index 0 out of bounds for length 0
Your append(char[], char)
method actually works. If you just run this example you will see there is no exception.
import java.util.*;
public class Test {
public static void main(String[] args){
char[] array = {};
append(array, 'b');
}
static void append(char[] array, char x) {
array = Arrays.copyOf(array, array.length + 1);
array[array.length - 1] = x;
}
}
So your error must be somewhere else. Most likely your append
method should return the newly generated array:
import java.util.*;
public class Test {
public static void main(String[] args){
char[] array = {};
array = append(array, 'b');
array = append(array, 'c');
}
static char[] append(char[] array, char x) {
array = Arrays.copyOf(array, array.length + 1);
array[array.length - 1] = x;
return array;
}
}
Firebase Cloud Functions: PubSub, "res.on is not a function"
TypeError: Cannot read properties of undefined (reading 'createMessageComponentCollector')
I am currently trying to use Brython for the website I am making, but I cannot get the data from the user for my program
I have a little problem, a little "classic" question, but I can't find an answer to this one
I am trying fetch request, if request takes time longer than specified timeout then I am aborting it using abort controller or if there is error in response like socket hangup then try to fetch it again recursivelyI want to repeat it i
Below is the Model of the database which I am using with mongoose and nodejs