这个 Java for 循环在伪代码中应该是什么样子的?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/20923806/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How this Java for loop should look like in pseudocode?
提问by user3116280
How should I proceed to turn this piece of code into 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(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);
}
}
I have no idea how to turn the for loops, add(), contains() and removeAll() methods into pseudocode.
我不知道如何将 for 循环、add()、contains() 和 removeAll() 方法转换为伪代码。
采纳答案by Jo?o Cunha
PseudoCode can be whatever you want. Since you or other people can understand what the lines mean.
伪代码可以是任何你想要的。因为您或其他人可以理解这些线条的含义。
You can turn it simple as FOR (your variable value start) TO (your end desired) i++ -
你可以把它变成简单的 FOR(你的变量值开始)TO(你想要的最终) i++ -
Basically something that makes people and you mostly understand that it is a For Loop
基本上是让人们和你大多理解它是一个 For 循环的东西
回答by A4L
How about something like this:
这样的事情怎么样:
for each element in list A
add element to list nonDup
Its basically just plain text which can be read by anyone. You could choose more speaking names for the variables. You could also choose begin loop
and end loop
to show the scope of the loop instead of indentication.
它基本上只是任何人都可以阅读的纯文本。您可以为变量选择更多说话的名称。您还可以选择begin loop
并end loop
显示循环的范围而不是缩进。
回答by Adam Stelmaszczyk
For example:
例如:
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
(my pseudocode style is somehow similar to Python...)
(我的伪代码风格有点类似于 Python ......)
In Java, to have only unique values, you can simply put all of them in a set:
在 Java 中,要只有唯一值,您可以简单地将所有值放在一个集合中:
Set<Integer> nonDup = new HashSet<Integer>(listA.addAll(listB));
回答by ADTC
It's just plain English:
这只是简单的英语:
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.
Normally you don't need to bother with pseudocodes. They really don't have much use (other than bragging rights... I mean,helping your peers understand your code) and they are not executable.
通常你不需要打扰伪代码。它们真的没有多大用处(除了吹牛……我的意思是,帮助您的同行理解您的代码)并且它们不可执行。