[英]I'm trying to remove all prime numbers from an arraylist, but nothing is printing?
I'm a bit of a newbie, so I apologize for any silly errors. 我是个新手,因此对任何愚蠢的错误表示歉意。
Here's the code: 这是代码:
public static void keepOnlyCompositeNumbers( List<Integer> nums ){
int num=0;
boolean isPrime=true;
for(int i=0; i<nums.size(); i++){
num=nums.get(i);
{
for (int o = 2; o < num; ++i)
{
if (num % o == 0){
isPrime=false;
o=2;
if(isPrime==true){
nums.remove(num);
}
}
}
}
And the runner class: 和亚军类:
Integer[] nums = {2,6,8,9,10,12,13,15,17,24,55,66,78,77,79};
List<Integer> list = new ArrayList<Integer>( Arrays.asList(nums) );
System.out.println( list );
ArrayListFunHouse.keepOnlyCompositeNumbers( list );
System.out.println( list );
Nothing's printing. 什么都没印刷。 Can somebody please tell me what I'm doing wrong, please?
有人可以告诉我我做错了吗? I have been stumped for quite some time.
我已经很困惑了一段时间。 Any help would be appreciated.
任何帮助,将不胜感激。 Thank you!
谢谢!
You can use like this: 您可以这样使用:
public static void keepOnlyCompositeNumbers( List<Integer> nums ){
boolean isPrime=false;
for(int i=0;i<nums.size();i++){
isPrime = checkPrime(nums.get(i));
if(isPrime){
nums.remove(i);
}
System.out.println(nums);
}
boolean checkPrime(int n) {
for(int i=2;i<n;i++) {
if(n%i==0)
return false;
}
return true;
}
Fetch each number in for loop and check in separate method for prime if return value is true which is prime number than remove it. 如果返回值是true(这是质数),则在for循环中获取每个数字并在单独的方法中检查质数,然后删除该质数。
You'r not getting output because of infinite loop 由于无限循环,您没有得到输出
for (int o = 2; o < num; ++i)
problem is you are increment i instead of o, and loop will get infinite when it get input which is greater than 2 问题是您是增量i而不是o,并且当输入大于2时,循环将变为无穷大
More efficent checkPrime 更有效的检查
boolean checkPrime(int n) {
for(int i=2;i<=n/2;i++) {
if(n%i==0)
return false;
}
return true;
}
Please have a look 请看一看
public static void main(String[] args) throws IOException {
Integer[] nums = { 2, 6, 8, 9, 10, 12, 13, 15, 17, 24, 55, 66, 78, 77, 79 };
List<Integer> list = new ArrayList<Integer>(Arrays.asList(nums));
System.out.println("Original List => " + list);
keepOnlyCompositeNumbers(list);
System.out.println("FInal List => " + list);
}
public static void keepOnlyCompositeNumbers(List<Integer> nums) {
for (int i = 0 ; i < nums.size() ; i++) {
if (isPrime(nums.get(i))) {
nums.remove(i);
}
}
}
public static boolean isPrime(int number) {
for (int i = 2 ; i < number ; i++) {
if (number % i == 0) {
return false; // number is divisible so its not prime
}
}
return true; // number is prime now
}
output 输出
Original List => [2, 6, 8, 9, 10, 12, 13, 15, 17, 24, 55, 66, 78, 77, 79]
原始列表=> [2,6,8,9,10,12,13,15,17,24,55,66,78,77,79]
FInal List => [6, 8, 9, 10, 12, 15, 24, 55, 66, 78, 77]
最终列表=> [6,8,9,10,12,15,24,55,66,78,77]
Working from Sarz's answer, something like this should work a little better for checkPrimes: 根据Sarz的答案,类似这样的东西对于checkPrimes应该会更好一些:
public static void keepOnlyCompositeNumbers( List<Integer> nums ){
boolean isPrime=false;
for(int i=0;i<nums.size();i++){
if ( checkPrime(nums.get(i), nums))
nums.remove(i);
}
boolean checkPrime(int n, List <Integer> primes) {
sqrtn = Math.sqrt(n) ;
for (Integer p : primes) {
if (p > sqrtn)
return true;
if (n % p == 0)
return false
}
}
There are a number of mistakes here. 这里有很多错误。 There may be others.
可能还有其他。
num=nums.get(i);
{
for (int o = 2; o < num; ++i)
{
if (num % o == 0){
isPrime=false;
o=2;
if(isPrime==true){
nums.remove(num);
}
}
}
1) for (int o = 2; o < num; ++i)
seems like a typo. 1)
for (int o = 2; o < num; ++i)
似乎是一个错字。 The loop will exit when o
becomes num
or greater. 当
o
变为num
或更大时,循环将退出。 But you're not incrementing o
, you're incrementing i
. 但是您没有增加
o
,而是增加i
。 Understandable since they're right next to each other on the keyboard, and the compiler didn't catch it since i
exists as a variable. 可以理解,因为它们在键盘上紧挨着,并且因为
i
作为变量存在,所以编译器没有捕获它。
2) Even if you change it to o++
, you sabotage your loop by setting o=2
if the number is non-prime. 2)即使将其更改为
o++
,如果数字不是素数,也可以通过将o=2
设置为破坏循环。 This means that o < num
will never be true, because you keep setting it back to 2, and the loop will go on forever. 这意味着
o < num
永远不会为真,因为您将其重新设置为2,并且循环将永远持续下去。
It appears that you're trying to say "this number is composite, so let's go to the next number". 您似乎想说的是“此数字是复合数字,所以我们转到下一个数字”。 But you're not.
但是你不是。
nums.remove(num)
removes num
from the list, but it does not change num . nums.remove(num)
从列表中删除num
, 但不会更改num 。 You haven't done anything to tell it to go to the next num
. 您没有做任何事情告诉它去下一个
num
。 There is nothing that changes num
inside the for
loop. 没有什么可以改变
for
循环内的num
的。 There's a statement that assigns to num
outside the for
loop, but that doesn't do you any good as long as you're stuck in the for
loop. 有指派给一个说法
num
外 for
循环,但这并不只要你停留在你有好处for
循环。
3) The isPrime==true
test is in the wrong place. 3)
isPrime==true
测试位置错误。 It will never succeed, because you've just set it to false
. 它永远不会成功,因为您只是将其设置为
false
。 The right place to test isPrime==true
is after you've checked every divisor. 在检查每个除数之后,正确的测试位置
isPrime==true
。 That means you have to move it outside the for
loop, after the closing curly brace. 这意味着您必须在大括号关闭之后将其移至
for
循环之外 。 (Also, you don't need to say if (isPrime==true)
; the usual way is just if (isPrime)
.) (此外,您不必说
if (isPrime==true)
;通常的方法就是if (isPrime)
。)
4) There's one more subtle error that trips up lots of people. 4)还有一个细微的错误使很多人绊倒。 If you have an index that goes through a list, and in the middle of that loop you remove an item from the list, you will miss numbers in the list.
如果您有一个遍历列表的索引,并且在该循环的中间从列表中删除了一个项目,那么您将错过列表中的数字。 Consider this:
考虑一下:
List<Integer> list = new ArrayList<>(Arrays.asList(10, 20, 30, 40));
for (int i = 0; i < list.size(); i++) {
int num = list.get(i);
if (needToDelete(num)) {
list.remove(i);
}
}
The first time through the loop, i
is 0 and num
is 10. Say we decide 10 needs to be deleted. 第一次循环,
i
为0, num
为10。假设我们决定删除10个。 So we remove it. 因此,我们将其删除。 The list is now [20, 30, 40].
现在的列表是[20,30,40]。 Then we go back, increment
i
to 1, and set num
to list.get(1)
--which is now 30! 然后我们返回,将
i
递增为1,并将num
设置为list.get(1)
,现在为30! So we never looked at the element 20. 所以我们从没有看过元素20。
There are a number of ways to write this differently. 有许多方法可以不同地编写此代码。 I don't like changing indexes in the middle of a
for
loop, so I'd do this: 我不喜欢在
for
循环中间更改索引,所以我会这样做:
int i = 0;
while (i < list.size()) {
num = list.get(i);
if (needToDelete(num)) {
list.remove(i);
} else {
i++;
}
}
Note that I increment i
only when I don't remove an element from the list. 请注意,仅当我没有从列表中删除元素时,我才递增
i
。 If I do, i
stays the same and will now be the index of the next element, since I've removed the earlier one. 如果我这样做,
i
保持不变,因为我已删除了前一个元素,所以它将成为下一个元素的索引。
1) Put boolean isPrime = true; inside for loop.
2) in for(int o = 2; o < num; ++i) // change ++i to ++o
3) comment out o=2; //o=2 and put a break;
4) put nums.remove(i); outside for loop
public static void keepOnlyCompositeNumbers(List<Integer> nums) {
int num = 0;
for (int i = 0; i < nums.size(); i++) {
boolean isPrime = true;
num = nums.get(i);
{
for (int o = 2; o < num; ++o) {
if (num % o == 0) {
isPrime = false;
// o=2;
break;
}
}
if (isPrime == true) {
nums.remove(i);
}
}
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.