Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/345.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/url/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
这个Java for循环在伪代码中应该是什么样子?_Java_Pseudocode - Fatal编程技术网

这个Java for循环在伪代码中应该是什么样子?

这个Java for循环在伪代码中应该是什么样子?,java,pseudocode,Java,Pseudocode,如何将这段代码转换为伪代码 ArrayList<Integer> check = new ArrayList<Integer>(); ArrayList<Integer> dup = new ArrayList <Integer> (); ArrayList<Integer> nonDup = new ArrayList <Integer> (); for (int i : listA) { nonDup.add(

如何将这段代码转换为伪代码

ArrayList<Integer> check = new ArrayList<Integer>();
ArrayList<Integer> dup = new ArrayList <Integer> ();
ArrayList<Integer> nonDup = new ArrayList <Integer> ();

for (int i : listA) {
    nonDup.add(i);
}
for (int i : listB) {
    nonDup.add(i);
}
for (int i : listA) {
    check.add(i);
}
for (int i : listB) {
    if (check.contains(i)) {
        dup.add(i);
        nonDup.removeAll(duplicates);                   
    }
}
ArrayList check=new ArrayList();
ArrayList dup=新的ArrayList();
ArrayList nonDup=新的ArrayList();
for(int i:listA){
加上(i);
}
for(int i:listB){
加上(i);
}
for(int i:listA){
勾选.加入(i);
}
for(int i:listB){
如果(检查包含(i)){
重复添加(i);
非备份删除所有(重复);
}
}

我不知道如何将for循环、add()、contains()和removeAll()方法转换为伪代码。

伪代码可以是任何您想要的。因为你或其他人都能理解这句话的意思

您可以将它简化为(您的变量值开始)到(您所需的结束)i++-
基本上是让人们和你都明白这是一个For循环的东西,像这样的东西怎么样:

for each element in list A
    add element to list nonDup
它基本上只是任何人都可以阅读的纯文本。您可以为变量选择更多的发音名称。您还可以选择
开始循环
结束循环
来显示循环的范围,而不是标识

例如:

getNonDup(listA, listB):
    nonDup = listA + listB
    dup    = an empty list
    for each object i in listB do:
        if listA contains i do:
            add i to dup
            remove i from nonDup
    return nonDup
(我的伪代码风格在某种程度上类似于Python…)

在Java中,要仅具有唯一值,只需将所有值放在一个集合中:

Set<Integer> nonDup = new HashSet<Integer>(listA.addAll(listB));
setnondup=newhashset(listA.addAll(listB));

这只是通俗易懂的英语

Initialize "check" as an empty (array-backed) list of integers.
Initialize "dup" as an empty (array-backed) list of integers.
Initialize "nonDup" as an empty (array-backed) list of integers.

For each integer in listA:
    Add the integer to "nonDup".
End of loop.

For each integer in listB:
    Add the integer to "nonDup".
End of loop.

For each integer in listA:
    Add the integer to "check".
End of loop.

For each integer in listB:
    If "check" contains the integer:
        Add the integer to "dup".
        Remove all integers in "dup" from "nonDup".
    End of if.
End of loop.

通常,您不需要为伪代码而烦恼。它们真的没有什么用处(除了吹牛的权利……我的意思是,帮助你的同事理解你的代码),而且它们不是可执行的。

伪代码只是简单的英语代码。因此,只需编写函数的功能。例如“从非备份中删除所有重复项”。通常会从一个伪代码到另一个代码…@Drew必须是家庭作业:))是的(:我事先完成了代码,因为我觉得编码很有趣,但伪代码没有那么多。但我确信我将来会学会适应。@user3116280请参阅我的答案:)通常,您不需要为伪代码而烦恼。它们真的没有多大用处(除了吹牛的权利,我的意思是,帮助你的同事理解你的代码),而且它们不是可执行的。好吧,我现在可以理解如何将函数变成伪代码了。但是对于(inti:listA){这篇文章将整数i转换为数组listA中的每个数字,您对此有何建议?谢谢,没有问题:)只是想寻求帮助。如果您愿意,您可以接受我的答案,以便其他可能有相同问题的用户可以更容易地找到答案