如何获得Java中阵列中的最后一个数字与零不同

发布于 2025-01-18 12:23:03 字数 570 浏览 2 评论 0原文

例如,我有这个数组:

double array1[] = {2.3, 5.0, 4.7, 8.2, 1.8, 9.0, 0, 0, 0, 0, 0, 0};

我想找到数组的最后一个不同于 0 的数字。在这种情况下,我的输出应该是: 9.0

我的代码是:

double array1[] = {2.3, 5.0, 4.7, 8.2, 1.8, 9.0, 0, 0, 0, 0, 0, 0};
double array2[] = array1.length;
double num;

for (int i = 0; i < array1.length; i++){
    array2[i] = array1[i];
    if(array2 != 0){
        num = array[i-1];
    }
} 

但它不起作用。

我需要强调的是 array1 的大小始终相同。但非零数字可以改变,在任何情况下,所有的零将始终位于数组的末尾。

我还需要有由 array1 获得的 array2。

此外,所有数字始终为正数。

I have for example this array:

double array1[] = {2.3, 5.0, 4.7, 8.2, 1.8, 9.0, 0, 0, 0, 0, 0, 0};

And I want to find the last number of the array different to 0. In this case, my output should be: 9.0

My code is:

double array1[] = {2.3, 5.0, 4.7, 8.2, 1.8, 9.0, 0, 0, 0, 0, 0, 0};
double array2[] = array1.length;
double num;

for (int i = 0; i < array1.length; i++){
    array2[i] = array1[i];
    if(array2 != 0){
        num = array[i-1];
    }
} 

But it doesn't work.

I need to highlight that the size of the array1 is always the same. But the non-zero numbers can change, in any case, all the zeros will be at the end of the array always.

I need also have array2, obtained by array1.

Furthermore, all the numbers are always positive.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

娇女薄笑 2025-01-25 12:23:03

您需要从数组的末尾开始,而不是开始:

public static double getLastNonZero(double [] data) throws Exception {
    for (int i = data.length-1; i >=0; i--) {
        if (data[i] != 0) {
            return data[i];
        }
    }
    
    throw new Exception("No value found");
}
public static void main(String[] args) {
    
    double array1[] = {2.3, 5.0, 4.7, 8.2, 1.8, 9.0, 0, 0, 0, 0, 0, 0};
    try {
        System.out.println(getLastNonZero(array1));         
    } catch (Exception e) {
        e.printStackTrace();
    }
}

You need to start at the end of the array, not the beginning:

public static double getLastNonZero(double [] data) throws Exception {
    for (int i = data.length-1; i >=0; i--) {
        if (data[i] != 0) {
            return data[i];
        }
    }
    
    throw new Exception("No value found");
}
public static void main(String[] args) {
    
    double array1[] = {2.3, 5.0, 4.7, 8.2, 1.8, 9.0, 0, 0, 0, 0, 0, 0};
    try {
        System.out.println(getLastNonZero(array1));         
    } catch (Exception e) {
        e.printStackTrace();
    }
}
城歌 2025-01-25 12:23:03

这是使用循环和三元运算符的一种方法(a ? b : c 表示如果 a 为 true,则返回 b,否则返回 c

double array1[] = {2.3, 5.0, 4.7, 8.2, 1.8, 9.0, 0, 0, 0, 0, 0, 0};
  • 将 Last 初始化为 0。
  • 迭代数组,
  • 如果当前值 (i) == 0,则返回 Last。
  • 否则返回我
double last = 0;
for(double i : array1) {
    last = i == 0 ? last : i;
}
System.out.println(last);

打印
9.0

Here is one way using a loop and the ternary operator (a ? b : c says if a is true return b, else return c)

double array1[] = {2.3, 5.0, 4.7, 8.2, 1.8, 9.0, 0, 0, 0, 0, 0, 0};
  • initialize last to 0.
  • iterate over array
  • if current value (i) == 0, return last.
  • else return i
double last = 0;
for(double i : array1) {
    last = i == 0 ? last : i;
}
System.out.println(last);

prints
9.0

何以笙箫默 2025-01-25 12:23:03

tl; dr

Arrays
    .stream( myArray ) 
    .filter( d -> d != 0 )
    .reduce( ( first , second ) -> second   ) 

doubleStream

不一定是最佳方法,但我们可以使用流。具体来说,doubleStream

从我们的数组产生流后,滤除零值。然后通过调用降低来检索最后一个元素。

我们最终得到了 optionalDouble 对象包含我们的结果double riginitive或为空,这意味着在我们的原始数组中找不到匹配。

double[] doubles = { 1.0d , 2.2d , 3.3d, 0d , 0d } ;
OptionalDouble result = 
    Arrays
    .stream( doubles ) 
    .filter( d -> d != 0 )
    .reduce( ( first , second ) -> second   ) 
;

转储到控制台。

System.out.println( result ) ;

请参阅此代码在iDeone.com 中实时。。

可选的二次[3.3]

tl;dr

Arrays
    .stream( myArray ) 
    .filter( d -> d != 0 )
    .reduce( ( first , second ) -> second   ) 

DoubleStream

Not necessarily the best approach, but we can use streams. Specifically, DoubleStream.

After producing the stream from our array, filter out zero values. Then retrieve the last element by calling reduce.

We end up with an OptionalDouble object that either contains our resulting double primitive or is empty which means no matches were found in our original array.

double[] doubles = { 1.0d , 2.2d , 3.3d, 0d , 0d } ;
OptionalDouble result = 
    Arrays
    .stream( doubles ) 
    .filter( d -> d != 0 )
    .reduce( ( first , second ) -> second   ) 
;

Dump to console.

System.out.println( result ) ;

See this code run live at IdeOne.com.

OptionalDouble[3.3]

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文