Java 将数组值向左移动
这是我关于这个方法的第二篇文章。方法说明如下: “将2个或更多空间的所有序列在 字符数组。如果删除了任何空格,则使用相同的数字 的空字符“\u0000”将填充 阵列。“ 该方法的参数是char数组。我已经成功地计算了重复空间的数量,但是,我一辈子都不知道如何在计算此类重复空间时向下移动值。最后,该方法应将重复空格的索引数替换为“\u0000”字符。这就是我到目前为止所做的:Java 将数组值向左移动,java,arrays,loops,for-loop,Java,Arrays,Loops,For Loop,这是我关于这个方法的第二篇文章。方法说明如下: “将2个或更多空间的所有序列在 字符数组。如果删除了任何空格,则使用相同的数字 的空字符“\u0000”将填充 阵列。“ 该方法的参数是char数组。我已经成功地计算了重复空间的数量,但是,我一辈子都不知道如何在计算此类重复空间时向下移动值。最后,该方法应将重复空格的索引数替换为“\u0000”字符。这就是我到目前为止所做的: // Calculate duplicate count before changing the array in
// Calculate duplicate count before changing the array
int duplicateCount = 0;
for(int i = 0; i + 1 < characters.length; i++){
if(characters[i] == ' ' && characters[i + 1] == ' '){
duplicateCount++;
}
}
// Shift the array down however much is needed
for(int j = 0; j + 1 < characters.length; j++){
if(characters[j] == ' ' && characters[j + 1] == ' '){
for(int a = j, b = a + 1; b < characters.length; a++, b++){
characters[a] = characters[b];
}
}
}
for(int replace = characters.length - duplicateCount; replace < characters.length; replace++){
characters[replace] = '\u0000';
}
}
//在更改数组之前计算重复计数
int duplicateCount=0;
for(int i=0;i+1
因此,如果输入是:char[]字符={'a','t','g','g','h',''}代码>
输出应该是:char[]expectedResult={'a','t','g','h','''''.\u0000','\u0000'}代码>
谢谢大家,这个问题看起来应该很简单,但我还是被卡住了。如果你能对你的回答作任何解释,我将不胜感激。
再次感谢。我猜这是你的家庭作业,你不应该使用任何图书馆之类的东西。这是我使用正则表达式的解决方案
import java.util.Arrays;
import java.util.regex.Pattern;
public class ReplaceMultipleWhiteSpace(){
public static void main(String[] args){
char[] array = new char[]{'a', ' ', ' ', ' ', ' ', ' ', 'b', ' ', 'c'};
String s = new String(array);
int length = s.length();
Pattern twoOrMoreWhiteSpace = Pattern.compile("( {2,})");
String replaced = twoOrMoreWhiteSpace.matcher(s).replaceAll(" ");
int diff = length - replaced.length();
char[] charArray = replaced.toCharArray();
char[] finalArray = Arrays.copyOf(charArray, replaced.length() + diff);
for (int i = replaced.length(); i < finalArray.length; i++) {
finalArray[i] = '\u0000';
}
System.out.println(Arrays.toString(array));
System.out.println(Arrays.toString(finalArray));
}
}
导入java.util.array;
导入java.util.regex.Pattern;
公共类ReplaceMultipleWhiteSpace(){
公共静态void main(字符串[]args){
字符[]数组=新字符[]{'a','','','','','','','',b','',c'};
字符串s=新字符串(数组);
int length=s.length();
patterntwoormorewitespace=Pattern.compile(({2,})”;
字符串替换=两个或多个空格.matcher.replaceAll(“”);
int diff=长度-替换为.length();
char[]charArray=replacement.toCharArray();
char[]finalArray=Arrays.copyOf(charArray,replaced.length()+diff);
for(int i=replaced.length();i
我猜这是你的家庭作业,你不应该使用任何库之类的东西。这是我使用正则表达式的解决方案
import java.util.Arrays;
import java.util.regex.Pattern;
public class ReplaceMultipleWhiteSpace(){
public static void main(String[] args){
char[] array = new char[]{'a', ' ', ' ', ' ', ' ', ' ', 'b', ' ', 'c'};
String s = new String(array);
int length = s.length();
Pattern twoOrMoreWhiteSpace = Pattern.compile("( {2,})");
String replaced = twoOrMoreWhiteSpace.matcher(s).replaceAll(" ");
int diff = length - replaced.length();
char[] charArray = replaced.toCharArray();
char[] finalArray = Arrays.copyOf(charArray, replaced.length() + diff);
for (int i = replaced.length(); i < finalArray.length; i++) {
finalArray[i] = '\u0000';
}
System.out.println(Arrays.toString(array));
System.out.println(Arrays.toString(finalArray));
}
}
导入java.util.array;
导入java.util.regex.Pattern;
公共类ReplaceMultipleWhiteSpace(){
公共静态void main(字符串[]args){
字符[]数组=新字符[]{'a','','','','','','','',b','',c'};
字符串s=新字符串(数组);
int length=s.length();
patterntwoormorewitespace=Pattern.compile(({2,})”;
字符串替换=两个或多个空格.matcher.replaceAll(“”);
int diff=长度-替换为.length();
char[]charArray=replacement.toCharArray();
char[]finalArray=Arrays.copyOf(charArray,replaced.length()+diff);
for(int i=replaced.length();i
您可以使用“输入”和“输出”的两个整数指针作为索引,通过数组一次将其复制到输入和输出数组中
char[] output = new char[input.length];
output[0] = input[0]; // Copy the first character, it's not a repeated space.
int ip = 1;
int op = 1;
while (ip < input.length) {
if (input[ip] != ' ' || input[ip - 1] != ' ') {
output[op++] = input[ip++];
} else {
ip++;
}
}
while (op < output.length) {
output[op++] = '\0';
}
char[]output=新字符[input.length];
输出[0]=输入[0];//复制第一个字符,它不是重复的空格。
int ip=1;
int op=1;
而(ip<输入长度){
如果(输入[ip]!=“输入[ip-1]!=”){
输出[op++]=输入[ip++];
}否则{
ip++;
}
}
while(op
您可以使用“输入”和“输出”的两个整数指针作为索引,通过数组一次将其复制到输入和输出数组中
char[] output = new char[input.length];
output[0] = input[0]; // Copy the first character, it's not a repeated space.
int ip = 1;
int op = 1;
while (ip < input.length) {
if (input[ip] != ' ' || input[ip - 1] != ' ') {
output[op++] = input[ip++];
} else {
ip++;
}
}
while (op < output.length) {
output[op++] = '\0';
}
char[]output=新字符[input.length];
输出[0]=输入[0];//复制第一个字符,它不是重复的空格。
int ip=1;
int op=1;
而(ip<输入长度){
如果(输入[ip]!=“输入[ip-1]!=”){
输出[op++]=输入[ip++];
}否则{
ip++;
}
}
while(op下面的代码应该很明显。您不需要计算需要在结尾处填充的字符数-只需填充符合条件的字符(=前一个字符和当前字符不都是“”),如果不符合条件,请不要复制它。最后,只需填充阵列的剩余部分
char[] result = new char[characters.length];
// copy the first character
result[0]=characters[0];
int resultIndex=1;
for(int i=1; i<characters.length; i++) {
// if the current character and the last character are not both spaces
if(!(result[resultIndex-1]==' ' && characters[i]==' ')) {
result[resultIndex++]=characters[i];
}
}
// fill the rest of the array with '\u0000'
while (resultIndex<characters.length) {
result[resultIndex++]='\u0000';
}
return result;
char[]result=新字符[characters.length];
//复制第一个字符
结果[0]=字符[0];
int resultIndex=1;
对于(int i=1;i而言,下面的代码应该非常明显。您不需要计算需要在结尾处填充的字符数-只需填充符合条件的字符(=前一个字符和当前字符都不是“”),如果不符合条件,则不要复制它。最后,只需填充数组的其余部分
char[] result = new char[characters.length];
// copy the first character
result[0]=characters[0];
int resultIndex=1;
for(int i=1; i<characters.length; i++) {
// if the current character and the last character are not both spaces
if(!(result[resultIndex-1]==' ' && characters[i]==' ')) {
result[resultIndex++]=characters[i];
}
}
// fill the rest of the array with '\u0000'
while (resultIndex<characters.length) {
result[resultIndex++]='\u0000';
}
return result;
char[]result=新字符[characters.length];
//复制第一个字符
结果[0]=字符[0];
int resultIndex=1;
对于(int i=1;i来说,就地执行非常简单。只需遍历数组,跟踪正在检查的索引(看看是否有多余的空间)和正在复制的索引。最后,用“\u0000”值填充数组,就完成了