简体   繁体   English

检查数组中的特定元素是否包含某个String。 然后删除该元素

[英]Check if specific element in array contains a certain String. Then delete the element

I have an ArrayList containing: [2x, 5, 6x]. 我有一个ArrayList包含:[2x,5,6x]。 I want to get rid of the elements containing the "x", but the contains() method applies to all of the elements in the ArrayList. 我想摆脱包含“x”的元素,但contains()方法适用于ArrayList中的所有元素。

My attempt: 我的尝试:

boolean ready = false;

while(!ready){
    if(first element in the array contains "x"){
    remove that element
    Check next element in line
}

When checked all the elements, and removed those which contains "x", set ready to true, and stop the loop. 检查所有元素,并删除包含“x”的元素时,将ready设置为true,并停止循环。 I want the output to just be: [6]. 我希望输出只是:[6]。 And then convert it to int, instead of String. 然后将其转换为int,而不是String。

EDIT: I couldn't get it to work with ArrayList, because I couldn't alter the contents of an ArrayList. 编辑:我无法使用ArrayList,因为我无法改变ArrayList的内容。 Therefore I changed it to a simple array. 因此我把它改成了一个简单的数组。 The array is now: String[]tokens = ligning2.split("-|\\+|\\*|\\/"); 数组现在是:String [] tokens = ligning2.split(“ - | \\ + | \\ * | \\ /”);

So the tokens array will keep [2x, 5, 6x]. 因此令牌数组将保持[2x,5,6x]。 So how do I delete the elements containing an x, with this kind of Array, instead of Arraylist? 那么如何使用这种数组而不是Arraylist删除包含x的元素呢?

This would be a great opportunity to utilize a lambda when calling List#removeIf . 这是调用List#removeIf时利用lambda的绝佳机会。

For example: 例如:

List<String> list = Arrays.asList("2x", "5", "6x");

System.out.println(list); // [2x, 5, 6x]

list.removeIf(element -> element.contains("x"));

System.out.println(list); // [5]

Assuming that there can be multiple values that remain in the List which did not contain an "x", you can use a Stream to map each element to its integer value: 假设有可以是保留在多个值List不含有一个“x”,则可以使用一个Stream来的每个元素映射到它的整数值:

List<Integer> newList = list.stream()
                            .map(Integer::valueOf)
                            .collect(Collectors.toList());

The entire algorithm can be condensed into the following: 整个算法可以压缩成如下:

List<Integer> newList = list.stream()
                            .filter(element -> !element.contains("x"))
                            .map(Integer::valueOf)
                            .collect(Collectors.toList());

Easiest thing to do is using Stream s. 最简单的方法是使用Stream These were added in Java 8 and you can use them in your case to filter certain items from the list and convert them to integers in one run: 这些是在Java 8中添加的,你可以在你的情况下使用它们来过滤列表中的某些项目并在一次运行中将它们转换为整数:

List<String> elements = Arrays.asList("2x", "5", "6x");
List<Double> result = elements.stream()
     .filter(entry -> !entry.contains("x"))
     .map(Double::valueOf) //this assumes that all other entries are valid doubles
     .collect(Collectors.toList());
  • Use an iterator to iterate the List 使用迭代器迭代List
  • for each iteration, if the current String contains the "x" String , remove it from the List with Iterator.remove(); 对于每次迭代,如果当前String包含“x” String ,则使用Iterator.remove();将其从List删除Iterator.remove();

For example : 例如 :

   for (Iterator<String> iterator = list.iterator(); iterator.hasNext();) {
        String value = list.next();
        if(value.contains("x"){
           iterator.remove();
        }
    }

You can use an Iterator for example : 例如,您可以使用Iterator:

List<String> list = Arrays.asList("Hello", "xyz", "tkx");
Iterator<String> iter = list.iterator();
while (iter.hasNext()) {
    if(iter.next().contains("x")){//check if your element contain x
        iter.remove();//if yes remove it
    }
}

You can use this program 你可以使用这个程序

package com.anand;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Iterator;
import java.util.ListIterator;

public class ArrayListRemove {
static ArrayList<String> al=new ArrayList<>(Arrays.asList("6x","8","5x"));
public static void main(String[] args) {
    Iterator<String> i=al.iterator();
    while(i.hasNext())
    {
        if(i.next().toString().contains("x"))
        {
            i.remove();
        }
    }
    ListIterator<String> li= al.listIterator();
    while(li.hasNext())
    {
        System.out.println(li.next());
    }

}

}

Please let me know of you want to create a new Arraylist of integers or just the output to be integers. 请让我知道你想创建一个新的整数Arraylist或只是输出整数。 I will make changes accordingly 我会相应地做出改变

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM