Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/algorithm/11.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 正在线程中获取异常";“主要”;错误_Java_Arrays_For Loop - Fatal编程技术网

Java 正在线程中获取异常";“主要”;错误

Java 正在线程中获取异常";“主要”;错误,java,arrays,for-loop,Java,Arrays,For Loop,所以我有一个包含这些数字的数组,我的任务是创建两个新数组,其中一个数组(poz)将包含数组中的所有正数,第二个数组(neg)将从数组中取出所有负值 public class obNiza { public static void main(String[] args) { int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87}; Arrays.sort(array); i

所以我有一个包含这些数字的数组,我的任务是创建两个新数组,其中一个数组(poz)将包含数组中的所有正数,第二个数组(neg)将从数组中取出所有负值

public class obNiza {
    public static void main(String[] args) {
     int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
     Arrays.sort(array);

     int[] poz= new int[6];
     int[] neg= new int[5];

     for(int i=0; i<array.length; i++)
     { 
         if(array[i] < 0)
             poz[i] = array[i];
         else neg[i] = array[i];
     }

       System.out.println(Arrays.toString(poz));
         System.out.println(Arrays.toString(neg));

    }
}
公共类obNiza{
公共静态void main(字符串[]args){
int[]数组={12,23,-22,0,43545,-4,-55,43,12,0,-999,-87};
数组。排序(数组);
int[]poz=新int[6];
int[]neg=新的int[5];

对于(int i=0;i,由于您事先不知道正数和负数的数量,因此最好使用
ArrayList

ArrayList<Integer> poz = new ArrayList<>();
ArrayList<Integer> neg = new ArrayList<>();
注意:如果希望0进入
poz

输出:

[12,12,23,43,43545]

[-999,-87,-55,-22,-4,0,0]


由于您事先不知道正数和负数的数量,因此最好使用
ArrayList

ArrayList<Integer> poz = new ArrayList<>();
ArrayList<Integer> neg = new ArrayList<>();
注意:如果希望0进入
poz

输出:

[12,12,23,43,43545]

[-999,-87,-55,-22,-4,0,0]


我知道您可以使用什么
List
ArrayList
):

import java.util.*;
公开课{
公共静态void main(字符串[]args){
int[]数组={12,23,-22,0,43545,-4,-55,43,12,0,-999,-87};
数组。排序(数组);
List positiveList=新建ArrayList();
List negativesList=new ArrayList();

对于(int i=0;iIMHO您可以使用
List
ArrayList
):

import java.util.*;
公开课{
公共静态void main(字符串[]args){
int[]数组={12,23,-22,0,43545,-4,-55,43,12,0,-999,-87};
数组。排序(数组);
List positiveList=新建ArrayList();
List negativesList=new ArrayList();

对于(int i=0;i您的
数组
大小是13。但是
poz
数组的大小+
neg
数组的大小=13。在for循环中,
数组
中的零值添加到
neg
数组中。因此引发
数组索引边界异常
。必须删除
数组
中的零值。因为零既不是零也不是零肯定或否定

要从数组中删除值,可以使用
ArrayUtils
class

例如:
array=ArrayUtils.removeElement(数组,元素)


您的
数组
大小是13。但是
poz
数组的大小+
neg
数组的大小=13。在for循环中,
数组
中的零值被添加到
neg
数组中。因此抛出
数组索引边界异常
。您必须删除
数组
中的零值。因为零既不是posi不积极也不消极

要从数组中删除值,可以使用
ArrayUtils
class

例如:
array=ArrayUtils.removeElement(数组,元素)



为什么poz和neg数组的大小是随机的?5和6是从哪里来的?
poz[i]
neg[i]
对于
i>4
i>5
不存在,因为这也将超出范围
array[i]<0
不是正数number@Stultuske
数组
的大小比其他任何一个数组都大。通过使用
i
数组
上循环,他将超过其他两个数组的限制,他尝试在处存储负值和正值,因为他在此处也使用
i
访问ind比如这些数组。@SomeJavaGuy那么我在int[]poz=new int[]中放了多大尺寸的数组;那么它就完成了我有点像乞丐一样困惑int[]neg=new int[];为什么poz和neg数组的大小是随机的?5和6是从哪里来的?
poz[i]
neg[i]
对于
i>4
i>5
不存在,因为这也将超出范围
数组[i]<0
不是正数number@Stultuske
数组
的大小比其他任何一个数组都大。通过使用
i
数组
上循环,他将超过其他两个数组的限制,他尝试在处存储负值和正值,因为他在此处也使用
i
访问ind比如这些数组。@SomeJavaGuy那么我应该在int[]poz=new int[]中放入多大的值;这样做我有点像begginer int[]neg=new int[]一样困惑;好的,这是在做一项工作,只需要再多几行,我如何在这段代码中应用systemoutprintln,因为sout(poz);不起作用
System.out.println(poz)
System.out.println(neg);
会像上面的框一样打印它们。它确实有效,非常感谢你,我还有一件事要做,那就是如何处理neg输出中的2 0,0,因为我在最终输出中不需要零这里有几种方法;一种方法是将
else
更改为
else if(数组[i]<0)
这工作完美无瑕真棒,我的老师接受了它,但告诉我我需要一个新的数组,它将从数组中复制并写入它们[+它有多少个副本]很抱歉打扰你这么多..好的,这是在做一项工作,只需要再多几行,我如何在这个代码中应用systemoutprintln,因为sout(poz);不工作
System.out.println(poz);
System.out.println(neg);
将按上面框中的方式打印它们。非常感谢,我还有一件事要做,那就是如何处理负输出中的2 0,0,因为我在最终输出中不需要零。这里有几种方法;一种是将
else
更改为
else if(数组[i]<0)
这真是天衣无缝,太棒了,我的老师接受了它,但告诉我我需要一个新的数组,该数组将包含数组中的副本,并将其写入[+它确实有多少副本]很抱歉打扰你这么多。。
import java.util.*;

public class ObNiza {
    public static void main(String[] args) {
    int[] array = {12, 23, -22, 0, 43, 545, -4, -55, 43, 12, 0, -999, -87};
    Arrays.sort(array);

    List<Integer> positivesList = new ArrayList<>();
    List<Integer> negativesList = new ArrayList<>();

    for(int i=0; i<array.length; i++) { 
        if (array[i]==0) { // since 0 is not negative nor positive 
            continue;
        }
        if(array[i] < 0) {
            positivesList.add(array[i]);
        } else {
            negativesList.add(array[i]);
        }
    }

    Integer[] poz = positivesList.toArray(new Integer[positivesList.size()]);
    Integer[] neg = negativesList.toArray(new Integer[negativesList.size()]);

    System.out.println(Arrays.toString(poz));
    System.out.println(Arrays.toString(neg));

    }
}