如何开发一个应该从右到左删除数组重复元素的代码? [英] how to develop a code which should delete duplicate element of an array from right to left?
本文介绍了如何开发一个应该从右到左删除数组重复元素的代码?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我已经开发了左右复制元素的代码,但是在从右到左复制时我对内存管理和数组排列有些困惑。
我开发上述问题的代码。但是我没有得到结果所以请帮助我获得结果...谢谢
I already develop code for duplication of element from left right but I am having some confusion on memory management and array arrangement during right to left duplication.
I develop code for above problem. but I am not getting result so please do help me in getting result...thank you
class PR
{
public static void main(String[] args)
{
int[] x = {10, 11, 23, 25, 22, 30, 10, 11, 25, 22};
int len = x.length;
for (int i = 0;i < x.length;i++)
{
System.out.print(x[i] + ",");
}
for (int i = x.length - 1;i > 0;i--)
{
for (int j = i - 1;j >= 0;)
{
if (x[j] == x[i])
{
for (int k = j;k > 0;k--)
{
x[k] = x[k - 1];
}
len--;
}
else
{
j--;
}
}
}
int[] y = new int[len];
for (int i = 0;i < len;i++)
{
y[i] = x[i];
}
System.out.println("after deleting duplicate element:");
for (int i = 0;i < y.length;i++)
{
System.out.print(y[i] + ",");
}
}
}
推荐答案
关注您的代码后,我只做了一些修改这项工作。
Following your code, only I made some changes to make this work.
class PR{
public static void main(String[] args) {
int[] x = {10, 11, 23, 25, 22, 30, 10, 11, 25, 22};
int len = x.length;
for (int i = 0; i < x.length; i++) {
System.out.print(x[i] + ",");
}
for (int i = x.length - 1; i > 0; i--) {
for (int j = i - 1; j >= 0;j--) {
if (x[j] == x[i]) {
for (int k = j; k < len - 1; k++) {
x[k] = x[k + 1];
}
len--;
}
}
}
int[] y = new int[len];
for (int i = 0; i < len; i++) {
y[i] = x[i];
}
System.out.println("\nafter deleting duplicate element:");
for (int i = 0; i < y.length; i++) {
System.out.print(y[i] + ",");
}
}
}
当然我不是说这是最好的解决方案,但是,你的想法是这个工作,只看一些原始代码中失败的细节。希望这有帮助。
Of course I don't say that this is the best solution but, with your idea this work, only take a look some details that fail in your original code. Hope this help.
它正在工作。
感谢Luis的回复。还有其他最好的解决方案吗?
it's working.
thanks Luis for your response. is there any other best solution??
这篇关于如何开发一个应该从右到左删除数组重复元素的代码?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文