程序问答   发布时间:2022-06-02  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了如何从另一个数组返回具有某个 int 索引的数组大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决如何从另一个数组返回具有某个 int 索引的数组?

开发过程中遇到如何从另一个数组返回具有某个 int 索引的数组的问题如何解决?下面主要结合日常开发的经验,给出你关于如何从另一个数组返回具有某个 int 索引的数组的解决方法建议,希望对你解决如何从另一个数组返回具有某个 int 索引的数组有所启发或帮助;

我是一名初学 Java 的学生,我一直在尝试编写一种方法来列出数组中某个 int 的所有索引。到目前为止,我所做的是将该 int 的值存储在另一个数组中的相应索引处,但我能做的最好的事情是将所有其他不等于原始 int 的索引的值设置为 -1。

我想我需要将值 i 存储在数组中并删除所有 -1,但我不知道该怎么做。顺便说一下,这些值是 -1,因为我在这个程序中的所有数组都包含 0-100 之间的整数。如果这个数组中可能的整数可以是任何数字,我会怎么做?

当然还有更简单或更有效的方法来做到这一点。

public static int[] maxValueIndex(int[] arr,int targetValue,int X) {
    
    int[] maxValue = new int[x];
    
    
    for (int i = 0; i < arr.length; i++) {
        if (arr[i] == targetvalue) {
            maxValue[i] = arr[i];
        } else {
            maxValue[i] = -1;
        }
        
    }       
    return maxValue;
    }

解决方法

@H_262_14@

如果我正确理解了您的查询,那么您需要一个包含所有索引 i 的数组,例如 arr[i]==targetValue。我们可以使用任何动态数据结构有效地实现这一点。例如,使用 ArrayList 并继续将所有需要的索引一一添加,然后将 List 转换为数组并返回它。
像这样:

List<Integer> index = new ArrayList<Integer>();
for (int i = 0; i < arr.length; i++)
{
    if (arr[i] == targetvalue)
        index.add( i );
}
int[] maxValue = index.stream().mapToInt(Integer::intvalue).toArray();
return maxValue;
,

如果只需要使用数组来解决这个任务,可能需要两遍才能创建一个只包含有效索引的压缩数组:

  1. 找到匹配的数量,然后创建并填充一个紧凑数组
Arrays.copyOf
  1. 创建一个相同长度的数组,填充它,然后使用 public static int[] getTargeTindexes(int targetValue,int ... arr) { int n = arr.length; int[] indexes = new int[n]; int targetCount = 0; for (int i = 0; i < n; i++) { if (arr[i] == targetvalue) { indexes[i] = i; targetCount++; } else { indexes[i] = -1; } } for (int i = 0,j = 0; j < targetCount && i < n; i++) { if (indexes[i] > -1) { indexes[j++] = i; } } return Arrays.copyOf(indexes,targetCount); // truncate bad indexes } 压缩:
int

此外,该方法的签名使用 vararg 将输入数组作为 int ... arr 的序列传递——那么 vararg 参数 public static int[] getTargeTindexes(int targetValue,int ... arr) { return IntStream.range(0,arr.length) // get stream of indexes .filter(i -> arr[i] == targetvalue) // keep only matching indexes .toArray(); // build output array } 应该是最后一个。


如果可以使用Stream API,可以方便地以声明方式解决任务:

title = soup.find_all('a','product-item-link')[:10]
price = soup.find_all('span',class_='price')[:10]

"""
SAMPLE OUTPUT
Pepsi Pepsi Max - 330Ml                                      LBP 17,999
Pepsi Regular Bottle 330ml                                   LBP 3,999
Pepsi Black Regular Bottle 330ml                             LBP 3,999
Pepsi Diet Bottle 2.25L                                      LBP 10,999
Pepsi Regular 2.25L                                          LBP 10,999
Pepsi Regular Can 185ml                                      LBP 4,999
Pepsi Diet Bottle 1.25L                                      LBP 8,999
Pepsi Diet Can 185ml                                         LBP 4,999
Pepsi Regular Bottle 1.25L                                   LBP 8,999
Pepsi Diet Pet - 330Ml                                       LBP 3,999
""""

大佬总结

以上是大佬教程为你收集整理的如何从另一个数组返回具有某个 int 索引的数组全部内容,希望文章能够帮你解决如何从另一个数组返回具有某个 int 索引的数组所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。