怎么从一个数组中提取正数并将它们移动到另一个数组中(Java)?

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

How to extract positive numbers from one array and move them to another in Java?

问题

我可能完全做错了,但我有一个数字数组,我想从该数组中提取正数并将它们放入另一个数组中。我可能也想对负数做同样的操作。

以下是我尝试过的(代码是错误的,但我目前卡在这里):

public static void main(String[] args) {
    int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
    
    int[] arrayPositive = Arrays.copyOf(array, 13);
    
    for (int i = 0; i < arrayPositive.length; i++) {
        if (i > 0) {
            System.out.println("" + i);
        }
    }
}

任何反馈将不胜感激提前感谢
英文:

I am probably doing it all wrong but I have an array of numbers and I would like to extract positive numbers from that array and put them into a separate array. I would potentially want to do the same with negative numbers.

Here's what I've tried (the code is wrong but I am stuck here at the moment):

public static void main(String[] args) {
 int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
 
 int [] arrayPositive = Arrays.copyOf(array, 13);
 
    for (int i = 0; i &lt; arrayPositive.length; i++) {
      if (i &gt; 0) {
          System.out.println(&quot;&quot; + i);
      }
    }

Any feedback would be more than welcome! Thanks in advance.

答案1

得分: 0

如果您想提取正数,您的条件语句应该检查每个数组元素是否大于0,就像下面的代码示例一样。

for (int i = 0; i < arrayPositive.length; ++i) {
    if (arrayPositive[i] > 0) {
        System.out.println("" + arrayPositive[i]);
    }
}
英文:

If you want to extract positive numbers, your if should check whether each array element is greater than 0, like in the following code.

for (int i = 0; i &lt; arrayPositive.length; ++i) {
      if (arrayPositive[i] &gt; 0) {
          System.out.println(&quot;&quot; + arrayPositive[i]);
      }
    }

答案2

得分: 0

    public static void main(String[] args) {
        int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
        int l = array.length;
        int[] arrayPositive = new int[l];
        int[] arrayNegative = new int[l];
        int i,j,k;
        i=j=k=0;
        for (i = 0; i < l; i++){
            if (array[i] > 0) {
                arrayPositive[j]=array[i];
                ++j;
            }
            else if(array[i] < 0){
                arrayNegative[k]=array[i];
                ++k;
            }
        }
        for (i = 0; i < l; i++){
            System.out.println(arrayPositive[i]+"\t" + arrayNegative[i]);
        }
    }
英文:

There you go

    public static void main(String[] args) {
        int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
        int l = array.length;
        int[] arrayPositive = new int[l];
        int[] arrayNegative = new int[l];
        int i,j,k;
        i=j=k=0;
        for (i = 0; i &lt; l; i++){
            if (array[i] &gt; 0) {
                arrayPositive[j]=array[i];
                ++j;
            }
            else if(array[i] &lt; 0){
                arrayNegative[k]=array[i];
                ++k;
            }
        }
        for (i = 0; i &lt; l; i++){
            System.out.println(arrayPositive[i]+&quot;\t&quot; + arrayNegative[i]);
        }
    }

答案3

得分: 0

public static void main(String[] args) {

  int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};

  int[] arrayPositive = new int[array.length];

  for (int i = 0; i < array.length; i++) {
    if (array[i] > 0) {
      System.out.println("" + array[i]);
      arrayPositive[i] = array[i];
    }
  }

}
英文:
public static void main(String[] args) {
  
  int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
  
  int [] arrayPositive = new int[array.length];
 
    for (int i = 0; i &lt; array.length; i++) {
      if (array[i] &gt; 0) {
          System.out.println(&quot;&quot;+array[i] );
          arrayPositive[i]=array[i];
      }
    }
 
}

huangapple
  • 本文由 发表于 2020年8月23日 00:53:56
  • 转载请务必保留本文链接:https://go.coder-hub.com/63538769.html
匿名

发表评论

匿名网友

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

确定