从数组中选择元素并将其打印到java中的新数组的程序

从数组中选择元素并将其打印到java中的新数组的程序,java,arrays,Java,Arrays,这是我在这里的第一篇帖子,我会尽量准确地回答我的问题。作为家庭作业,我必须解决的问题是: 我们创建了一个数组,必须制作一个程序来选择值在-2.99到2.99之间的元素,并在新数组中打印它们。(爪哇) 我试过这个: 导入java.util.array public class Task14 { public static void main(String[] args) { double[] array = { -1.1, 4.567, 0.45, 7.77, 2.01,

这是我在这里的第一篇帖子,我会尽量准确地回答我的问题。作为家庭作业,我必须解决的问题是: 我们创建了一个数组,必须制作一个程序来选择值在-2.99到2.99之间的元素,并在新数组中打印它们。(爪哇) 我试过这个: 导入java.util.array

public class Task14 {

    public static void main(String[] args) {
        double[] array = { -1.1, 4.567, 0.45, 7.77, 2.01, -15.998, -0.004, 2.99, 3.005 };
        int count = 0;
        for (int i = 0; i < array.length; i++) {
            if ((array[i] >= -2.99) && (array[i] <= 2.99)) {
                count++;
            }
        }
        double[] interval = new double[count];
        for (int index = 0; index < array.length; index++) {
            if ((array[index] >= -2.99) && (array[index] <= 2.99)) {
// and here's the drama - i can't find a solution how to match the elements from the two arrays
//I tried 
interval[index] = array[index];  
//or to make a new variable which takes the value from array[index] when it's in -2.99 : 2.99, but it doesn't work either :) 
            }
        }
        System.out.println(Arrays.toString(interval));
    }
}
公共类任务14{
公共静态void main(字符串[]args){
双[]数组={1.1,4.567,0.45,7.77,2.01,-15.998,-0.004,2.99,3.005};
整数计数=0;
for(int i=0;i如果((数组[i]>=-2.99)&&&(数组[i]=-2.99)&&(数组[index]如果要继续沿着这条路径解决问题(而不使用
ArrayList
)考虑添加<代码> Currutsindex < /Cord>变量,以跟踪当前正在使用的新索引。每当您在范围内找到一个值时,这可以递增,例如:

double[] interval = new double[count];
int currentIndex = 0;
for (int index = 0; index < array.length; index++) {
    if ((array[index] >= -2.99) && (array[index] <= 2.99)) {
        interval[currentIndex] = array[index];
        currentIndex++;
    }
}
double[]间隔=新的double[count];
int currentIndex=0;
for(int index=0;index=-2.99)和&(数组[index]
List-filteredList=new-ArrayList();
for(双val:array){

如果((val>=-2.99)和&(val您需要保留
间隔
数组的索引:

int intervalIndex = 0;
for(a : array) {
    if(Math.abs(a) <= 2.99) {
        interval[intervalIndex++] = a;
    }
}

您可以通过一种节省空间的方式来实现这一点:

public static void main(String[] args) {
double[] array = { -1.1, 4.567, 0.45, 7.77, 2.01, -15.998, -0.004, 2.99, 3.005 };
double[] arrayNew = new double[array.length];
for (int i = 0,k=0; i < array.length; i++) {
    if ((array[i] >= -2.99) && (array[i] <= 2.99)) {
        arrayNew[k] = array[i];
        k++;
    }
}
publicstaticvoidmain(字符串[]args){
双[]数组={1.1,4.567,0.45,7.77,2.01,-15.998,-0.004,2.99,3.005};
double[]arrayNew=新的double[array.length];
for(int i=0,k=0;i如果((数组[i]>=-2.99)&&&(数组[i]=-2.99)&&(数组[i]谢谢!这正是我需要的:)提示:和你的老师谈谈。
double[] array = // ...
return Arrays.stream(array)
             .filter(d => Math.abs(a) <= 2.99)
             .toArray()
public static void main(String[] args) {
double[] array = { -1.1, 4.567, 0.45, 7.77, 2.01, -15.998, -0.004, 2.99, 3.005 };
double[] arrayNew = new double[array.length];
for (int i = 0,k=0; i < array.length; i++) {
    if ((array[i] >= -2.99) && (array[i] <= 2.99)) {
        arrayNew[k] = array[i];
        k++;
    }
}
public static void main(String[] args) {
double[] array = { -1.1, 4.567, 0.45, 7.77, 2.01, -15.998, -0.004, 2.99, 3.005 };
Vector<Double> arrayNew = new Vector<Double>();
for (int i = 0; i < array.length; i++) {
    if ((array[i] >= -2.99) && (array[i] <= 2.99)) {
        arrayNew.add(array[i]);
    }
}