Java 删除数组中某个位置的字符
我要做的是删除数组中某个位置的字符。该数组称为word removecharacter是一个int word是由字符串组成的数组 已经编写了一个测试程序,用户可以在其中键入int(removecharacter),该程序将给出在数组中删除项的位置 我认为我在正确的轨道上,但不确定某一行,这是实际的删除行。有什么建议吗Java 删除数组中某个位置的字符,java,arrays,Java,Arrays,我要做的是删除数组中某个位置的字符。该数组称为word removecharacter是一个int word是由字符串组成的数组 已经编写了一个测试程序,用户可以在其中键入int(removecharacter),该程序将给出在数组中删除项的位置 我认为我在正确的轨道上,但不确定某一行,这是实际的删除行。有什么建议吗 public boolean delCharAt(int removecharacter) { if (removecharacter <= word.length
public boolean delCharAt(int removecharacter) {
if (removecharacter <= word.length && delete >= 0)
{
//I wish to delete the character at removecharacter
}
public boolean delCharAt(int-removecharacter){
如果(removecharacter=0)
{
//我希望删除removecharacter处的角色
}
<> Po>从哪里开始?感谢< /P> < P>如果删除数组中的元素,则应该考虑使用(数组)列表。您将有一种方法从列表中删除对象或索引中的元素。尽量避免重新创建轮盘。 以下是Javadoc:
也可以,因为你的词来自一个字符串,你可以使用,你有一个方法叫做.如果你删除数组中的元素,你应该考虑使用(数组)列表。你将有一个方法来从列表中删除一个对象或一个索引中的一个元素。尽量避免重新创建轮子。< /P> 以下是Javadoc:
也可以,因为你的词来自一个字符串,你可以使用,你有一个方法叫做.< /p> 如果你想要添加和移除的多功能性,你可以考虑使用ARARYList或List,它们都有用于该任务的内置函数。
如果你绝对要使用数组,你也必须为我所使用的数组的长度存储一个值。
如果你想增加和移除的通用性,你可以考虑使用ARARYList或List,它们都有用于该任务的内置函数。如果必须使用数组,还必须为我使用的数组的长度存储一个值。完成此任务的一种方法是向下移动删除值后的所有值。然后可以将新的空插槽设置为null
if(removecharacter <= word.length && removecharacter >= 0)
{
for(int i=removecharacter+1; i<word.length; i++) {
word[i-1] = word[i];
word[i] = '\u0000'; // This will make sure that no duplicates are created in this
// process.
}
}
if(removecharacter=0)
{
对于(int i=removecharacter+1;i完成此任务的一种方法是向下移动删除值之后的所有值。然后可以将新的空槽设置为null
if(removecharacter <= word.length && removecharacter >= 0)
{
for(int i=removecharacter+1; i<word.length; i++) {
word[i-1] = word[i];
word[i] = '\u0000'; // This will make sure that no duplicates are created in this
// process.
}
}
if(removecharacter=0)
{
对于(int i=removecharacter+1;i我支持所有其他人说使用类似于ArrayList
,但是如果您没有选择,您可以使用System.arraycopy
将内容从原始数组复制到新的临时数组,并将结果分配回原始(word
)
这将减小数组的大小并删除字符
public class ArrayDelete {
// This is because I'm to lazy to build the character
// array by hand myself...
private static String text = "This is an example of text";
private static char word[] = text.toCharArray();
public static void main(String[] args) {
System.out.println(new String(word));
delete(9);
System.out.println(new String(word));
}
public static void delete(int charAt) {
if (word.length > 0 && charAt >= 0 && charAt < word.length) {
char[] fix = new char[word.length - 1];
System.arraycopy(word, 0, fix, 0, charAt);
System.arraycopy(word, charAt + 1, fix, charAt, word.length - charAt - 1);
word = fix;
}
}
}
我和其他任何人一样,都希望使用类似于ArrayList
,但如果您没有选择,可以使用System.arraycopy
将内容从原始数组复制到新的临时数组,并将结果分配回原始(word
)
这将减小数组的大小并删除字符
public class ArrayDelete {
// This is because I'm to lazy to build the character
// array by hand myself...
private static String text = "This is an example of text";
private static char word[] = text.toCharArray();
public static void main(String[] args) {
System.out.println(new String(word));
delete(9);
System.out.println(new String(word));
}
public static void delete(int charAt) {
if (word.length > 0 && charAt >= 0 && charAt < word.length) {
char[] fix = new char[word.length - 1];
System.arraycopy(word, 0, fix, 0, charAt);
System.arraycopy(word, charAt + 1, fix, charAt, word.length - charAt - 1);
word = fix;
}
}
}
delete
variable代表什么?请给出一个完整的示例。delete和word.Edited*的类型是什么?希望这对位有帮助您必须使用char数组,然后创建一个新字符串或获取beforesting+afterString..类似于..string beforesting=myString.substring(0,num);String afterString=myString.substring(num+1,myString.length()-beforesting.length()-1)是“word”字符串或字符数组?delete
variable代表什么?请给出一个完整的示例。delete和word的类型是什么。Edited*希望这可能对位有所帮助。您必须使用字符数组,然后创建一个新字符串或获取beforesting+afterString..类似于..String beforesting=myString.substring(0,num);String afterString=myString.substring(num+1,myString.length()=null,我无法从null转换为char抱歉,你是对的。我想的是C。你可以将其设置为character的默认值:'\u0000'。根据你的说明,另一种方法是复制数组。制作一个大小为word.length的临时数组。将单词数组复制到临时数组。然后说word=new char[temp.length-1]。这样,您可以复制除要删除的值以外的所有值。这很有意义,我尝试了Gennaro,但使用了:word[I]=null,我无法从null转换为char抱歉,你是对的。我想的是C。你可以将其设置为character的默认值:'\u0000'。根据你的说明,另一种方法是复制数组。制作一个大小为word.length的临时数组。将单词数组复制到临时数组。然后说word=new char[temp.length-1]。这样,您可以复制所有值,但要删除的值除外。