I am just revising java.lang and I have a method where it looks for the integers in a string and it identifies them leaving out the non-integers. How would I then remove all of the remaining "empty slots" in the array. I don't know if the code will help but I am going to put it anyways.
public static char[] lookForNums(@NotNull String str){
char[] strPointer = str.toCharArray();
char[] getNums = new char[str.length()];
for(int i = 0; i < str.length(); i++){
// point to X value in the array/string
char pointer = strPointer[i];
if(Character.isDigit(pointer)){
getNums[i] = pointer;
}
}
// Refinement/Garbage collection
for(int i = 0; i < getNums.length; i++){
if(getNums[i] == ' '){
getNums[i] = 'n'; // n as null placeholder (for nothing)
}
}
return getNums;
}// The challenge was I cant use String.charAt() or String.indexOf()
As you can see that garbage collection part pretty much is useless. Thanks for reading! I hope this is not a duplicate.