如何获取字符串数组元素的索引?

发布于 2025-01-28 13:39:30 字数 264 浏览 3 评论 0原文

我在String [] Strarray中有以下数据

1、9、3、4、16、6、7、8、2、10、11、12、12、13、45

我想找到包含3的元素的索引(例如:3 ,13)。即,我应该有3 = str [2]和13 = str [12],仅使用 java 8 features “ test”

I have the below data in a String[] strArray

1, 9, 3, 4, 16, 6, 7, 8, 2, 10, 11, 12, 13, 45

I want to find the index of those elements that contain 3 (e.g:3,13). i.e I should have 3=str[2] and 13=str[12] with just use Java 8 features like stream and etc. because I want after finding their index, to replace them with specific string for example "Test"

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

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

发布评论

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

评论(2

梦断已成空 2025-02-04 13:39:30

这是一种方法。使用三元运算符(?:)来决定是否应用test或原始值替换当前字符串。

String[] vals = { "1", "9", "3", "4", "16", "6", "7", "8",
        "2", "10", "11", "12", "13", "45" };

vals = Arrays.stream(vals)
        .map(str -> str.contains("3") ? "Test" : str)
        .toArray(String[]::new);

System.out.println(Arrays.toString(vals));

打印

[1, 9, Test, 4, 16, 6, 7, 8, 2, 10, 11, 12, Test, 45]

以将数组元素的地图返回到您可以这样做的找到的匹配项索引。这将处理具有不同索引的重复值。

  • 传输
  • 的值的索引流
  • 使用匹配索引将索引转换为整数(通过盒装 - 收集器所需的盒子)
  • 。然后使用过滤索引分配映射。该值是找到的每个实例的列表。
String[] vals = { "1", "9", "3", "4", "16", "6", "7", "8",
        "2", "10", "11", "12", "13", "45", "43", "3", "13" };

Map<String, List<Integer>> results = IntStream.range(0,vals.length)
        .filter(i -> vals[i].contains("3"))
        .boxed()
        .collect(Collectors.groupingBy(i->vals[i]));
 
results.forEach((k,v)-> System.out.println(k + " -> " + v));

打印

13 -> [12, 16]
3 -> [2, 15]
43 -> [14]

以用地图中的值索引替换原始数组,请执行以下操作。

for (String key : results.keySet()) {
    for (int i : results.get(key)) {
        vals[i] = i+"";
    }
}

Here is one way. Use the ternary operator (?:) to decide if the current string should be replaced with Test or the original value.

String[] vals = { "1", "9", "3", "4", "16", "6", "7", "8",
        "2", "10", "11", "12", "13", "45" };

vals = Arrays.stream(vals)
        .map(str -> str.contains("3") ? "Test" : str)
        .toArray(String[]::new);

System.out.println(Arrays.toString(vals));

prints

[1, 9, Test, 4, 16, 6, 7, 8, 2, 10, 11, 12, Test, 45]

To return a map of array element to index for the found matches you can do it like so. This will handle duplicate values with different indices.

  • Stream the indices of the array
  • filter the indices for values that match
  • convert the index to an Integer (via boxed - required for collector) using grouping by.
  • then assign to map using the filtered indices. The value is a list of where each instance of the key was found.
String[] vals = { "1", "9", "3", "4", "16", "6", "7", "8",
        "2", "10", "11", "12", "13", "45", "43", "3", "13" };

Map<String, List<Integer>> results = IntStream.range(0,vals.length)
        .filter(i -> vals[i].contains("3"))
        .boxed()
        .collect(Collectors.groupingBy(i->vals[i]));
 
results.forEach((k,v)-> System.out.println(k + " -> " + v));

prints

13 -> [12, 16]
3 -> [2, 15]
43 -> [14]

To replace the original array with the indices of its values in the map, do the following.

for (String key : results.keySet()) {
    for (int i : results.get(key)) {
        vals[i] = i+"";
    }
}
烟─花易冷 2025-02-04 13:39:30

您可以将阵列的长度从i = 0迭代到i&lt;长度。在循环中,将str [i]转换为字符串,并使用java string contains()方法检查该字符串是否包含“ 3”,如果是的话,您可以在Array str [i] =“ test”的位置替换

。可以使用Java流方法,地图做同样的事情。

str.stream().map(x-> {if(String.valueOf(x).contains(“3”)) return “Test”;}).collect(Collectors.toList());

在这种情况下,您可能需要使用.toArray而不是收集作为列表。

You can take the length of the array and iterate from i=0 to i<length. In the loop convert str[i] to a String and check if that string contains “3” using the Java String Contains() method, if so you can replace at that spot in the array str[i]=“Test”

Alternatively you can do the same thing using the Java Stream method, Map.

str.stream().map(x-> {if(String.valueOf(x).contains(“3”)) return “Test”;}).collect(Collectors.toList());

You may want to use .toArray rather than collect as a list in this case.

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