将整数在带有条件的 for 循环后保存到向量中

huangapple go评论78阅读模式
英文:

Save integer into vector after foor loop with conditionals

问题

以下是您提供的代码的翻译部分:

import java.io.*;
import java.util.Arrays;
import java.util.*;

class Even_odd_array {

    static void CountingEvenOdd(int arr[], int arr_size) {
        int even_count = 0;
        int odd_count = 0;

        Vector<Integer> vector_odd = new Vector<Integer>();
        Vector<Integer> vector_even = new Vector<Integer>();

        // 循环读取数组中的所有值
        for (int i = 0; i < arr_size; i++) {
            // 检查一个数字是否能被 2 整除
            if ((arr[i] & 1) == 1) {
                odd_count++;
                vector_odd.addElement(arr[i]);
            } else {
                even_count++;
                vector_even.addElement(arr[i]);
            }
        }

        System.out.println("偶数元素个数 = " + even_count + " 奇数元素个数 = " + odd_count);

        System.out.println(vector_odd); // 此向量应类似于 [1, 3, 5, 7]
        System.out.println(vector_even); // 此向量应类似于 [2, 4, 6, 8, 10]
    }

    // 主函数
    public static void main(String[] args) {
        int arr[] = {2, 4, 6, 8, 10, 1, 3, 5, 7};
        int n = arr.length;
        CountingEvenOdd(arr, n);
    }
}

输出应该是:

偶数元素个数 = 5 奇数元素个数 = 4
[1, 3, 5, 7]
[2, 4, 6, 8, 10]

关于您在 else 语句中使用 vector.add() 方法时出现错误的问题,您提到您不理解错误的原因。从代码来看,您的使用是正确的。如果您能提供更多关于错误的详细信息,我可能能够帮助您找到问题所在。

英文:

I have to set up a for loop that differences between odd and even numbers. I'm thinking to use an if/else statement into the for loop that evaluates de residue to know if is even or odd. Once done it, i'd like to save the number in a vector: one vector for the if condition, and another for the else.

This is my code!

import java.io.*;
import java.util.Arrays; 
import java.util.*;
  
class Even_odd_array { 
  
    static void CountingEvenOdd(int arr[], 
                              int arr_size) 
    { 
        int even_count = 0;          
        int odd_count = 0;  
        
        //int [] subvector_odd = {};
        Vector&lt;Integer&gt; vector_odd = new Vector&lt;Integer&gt;(); 
        Vector&lt;Integer&gt; vector_even = new Vector&lt;Integer&gt;(); 

              
        // loop to read all the values in 
        // the array 
        for(int i = 0 ; i &lt; arr_size ; i++)  
        { 
            // checking if a number is  
            // completely divisible by 2 
            if ((arr[i] &amp; 1) == 1) 
                odd_count ++ ; 
            	vector_odd.addElement(arr[i]);
            	
            else                
                even_count ++ ; 
            	vector_even.addElement(arr[i]);
        } 
      
        System.out.println( &quot;Number of even&quot;
               + &quot; elements = &quot; + even_count  
               + &quot; Number of odd elements = &quot;
                              + odd_count) ; 
        
        System.out.println(vector_odd); //This vector should look like [1, 3, 5, 7]
        
       
        
    } 
      
    // Driver Code 
    public static void main (String[] args)  
    { 
        int arr[] = {2, 4, 6, 8, 10, 1, 3, 5, 7}; 
        int n = arr.length; 
        CountingEvenOdd(arr, n); 
    } 
} 
  

The output should be:

Number of even elements = 5 Number of odd elements = 4
even_vector = [2, 4, 6, 8, 10]
odd_vector = [1, 3, 5, 7]

I'm getting an error in the else statement line when I include the vector.add() method inside the if statement, and I don't understand why...

Thanks!!

答案1

得分: 0

目前你的 else 块与任何 if 条件无关(如果块只包含一条语句,则可以省略大括号)。

要修复这个问题,使用大括号来界定 ifelse 块:

if ((arr[i] & 1) == 1) {
    odd_count++;
    vector_odd.addElement(arr[i]);
} else {
    even_count++;
    vector_even.addElement(arr[i]);
}

你当前的代码等效于:

if ((arr[i] & 1) == 1) {
    odd_count++;
}
vector_odd.addElement(arr[i]);
else {
    even_count++;
}
vector_even.addElement(arr[i]);

正如你所看到的,这里的 else 部分与 if 部分无法关联起来。

英文:

Currently your else block is not related to any if condition (you can omit curly braces if the block contains a single statement, though) .

To fix this, use curly braces to delimit the if and else blocks :

    if ((arr[i] &amp; 1) == 1) {
        odd_count ++ ; 
        vector_odd.addElement(arr[i]);
    }
    else {                
        even_count ++ ; 
        vector_even.addElement(arr[i]);
    }

Your current code is equivalent to :

if ((arr[i] &amp; 1) == 1) {
    odd_count ++ ;
} 

vector_odd.addElement(arr[i]);

else {                
    even_count ++ ; 
}

vector_even.addElement(arr[i]);

As you see, the else part can't be related to the if part here.

答案2

得分: 0

Modulo运算符为%而不是位与运算符&amp;参见https://www.tutorialspoint.com/java/java_basic_operators.htm)。此外,数组长度无需作为参数给出,并且不需要for循环。通过使用以下两行代码,您拥有所需的一切。

    Integer[] array = {2, 4, 6, 8, 1, 3, 7};
    Vector<Integer> vectorEven = Arrays.stream(array).filter(i -> i % 2 == 0).collect(Collectors.toCollection(Vector::new));
    Vector<Integer> vectorOdd = Arrays.stream(array).filter(i -> i % 2 == 1).collect(Collectors.toCollection(Vector::new));

    System.out.println(vectorEven);
    System.out.println(vectorOdd.size());
英文:

Modulo operator is %, not & which is bitwise and (see https://www.tutorialspoint.com/java/java_basic_operators.htm ). In addition, array length does not need to be given as parameter and a for loop is not needed. You have everything you need by using the two lines below.

Integer[] array = {2, 4, 6, 8, 1, 3, 7};
    Vector&lt;Integer&gt; vectorEven = Arrays.stream(array).filter(i -&gt; i % 2 == 0).collect(Collectors.toCollection(Vector::new));
    Vector&lt;Integer&gt; vectorOdd =Arrays.stream(array).filter(i -&gt; i % 2 == 1).collect(Collectors.toCollection(Vector::new));

    System.out.println(vectorEven);
    System.out.println(vectorOdd.size());

huangapple
  • 本文由 发表于 2020年9月23日 21:01:01
  • 转载请务必保留本文链接:https://go.coder-hub.com/64028575.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定