完成此方法 [英] Complete this method

查看:85
本文介绍了完成此方法的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要完成这个java方法。由于某种原因,整个阵列都被删除了。



我尝试过:



I need to complete this java method. For some reason the entire array gets deleted.

What I have tried:

/**
* Reduces all sequences of 2 or more spaces to 1 space within the
* characters array. If any spaces are removed then the same number
* of Null character '\u0000' will fill the elements at the end of the
* array.
*
* @param characters The series of characters that may have more than one
*     space in a row when calling. On return the array of characters will
*     not have more than one space in a row and there may be '\u0000'
*     characters at the end of the array.
*/
public static void removeDuplicateSpaces(char[] characters) {

	for (int i = 0; i < characters.length - 1; i++) {
		if (characters[i] == ' ' && characters[i + 1] == ' ') {
			int k = i + 1;
			int n = 0;
			char j='\u0000';
			while (n<characters.length-1){>
				for (k=i+1; k < characters.length-1; k++){
					if (characters[k]==' '){
						characters[k]=j;
					}
					for ( int l = 0; l < characters.length-1;l++){
						characters[l]=characters[l+1];
					}
				}
				n++;
			}
		}
	}

推荐答案

你应该学会尽快使用调试器。而不是猜测你的代码在做什么,现在是时候看到你的代码执行并确保它完成你期望的。



调试器允许你跟踪执行逐行检查变量,你会看到有一点它会停止你所期望的。

在Visual Studio 2010中掌握调试 - 初学者指南 [ ^ ]

http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html [ ^ ]

https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html [ ^ ]



想想你将如何手工操作,与你在代码中的操作进行比较,注意细节。
You should learn to use the debugger as soon as possible. Rather than guessing what your code is doing, It is time to see your code executing and ensuring that it does what you expect.

The debugger allow you to follow the execution line by line, inspect variables and you will see that there is a point where it stop doing what you expect.
Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
http://docs.oracle.com/javase/7/docs/technotes/tools/windows/jdb.html[^]
https://www.jetbrains.com/idea/help/debugging-your-first-java-application.html[^]

Think how you would do by hand, compare with what you do in your code, be careful to details.


char j='\u0000';
while (n<characters.length-1){>
	for (k=i+1; k < characters.length-1; k++){
		if (characters[k]==' '){
			characters[k]=j;
		}



您正在用空值替换空格,表示字符串的结尾。现在将忽略除null之外的所有字符。您应该删除重复的空格,并在数组末尾添加空字符,其中前一个字符已被移动。



您应该移动剩余的字符数组中的数组,覆盖重复的空格。然后根据覆盖的空格数在aray的末尾添加空值。在转换为代码之前,首先尝试将其写在纸上,以确切了解每个步骤应该做什么。


You are replacing a space with a null which indicates the end of the string. All characters beyond the null will now be ignored. You are supposed to remove duplicate spaces, and add null characters at the end of the array where the previous characters have been moved from.

You should move the remaining characters in the array up the array, overwriting the repeated spaces. You then add nulls at the end of the aray according to the number of spaces overwritten. Try writing it down on paper first to see exactly what each step should do, before converting to code.


这篇关于完成此方法的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆