简体   繁体   English

摆脱ArrayList中的空元素

[英]Getting rid of empty elements in ArrayList

I've been working on this question for a while and I'm still stumped. 我已经在这个问题上研究了一段时间了,但仍然感到困惑。

I'm supposed to write a method stutter that takes an ArrayList<String> as a parameter and that replaces every string with two of that string. 我应该编写一个以ArrayList<String>作为参数并将每个字符串替换为该字符串中的两个的方法。 For example, if the list stores the values {"how", "are", "you?"} before the method is called, it should store the values {"how", "how", "are", "are", "you?", "you?"} after the method finishes executing. 例如,如果列表在调用方法之前存储了值{"how", "are", "you?"} ,则应该存储值{"how", "how", "are", "are", "you?", "you?"}在方法完成执行之后。

But despite my best efforts I still can't seem to get rid of the empty elements that are in my code. 但是尽管尽了最大的努力,我仍然似乎无法摆脱代码中的空元素。

Any help would be greatly appreciated. 任何帮助将不胜感激。

public static ArrayList<String> stutter(ArrayList<String> lst) {
    ArrayList<String> list = new ArrayList<String>();
    int size    = lst.size();
    int intSize = lst.size();
    int inSize  = lst.size();
    int size4   = list.size();
    if (lst.size() == 0) {
       lst.clear();
    } else {
        for (int x = 0; x < size; x++) {
            for (int i = 0; i < 2; i++) {
                lst.add(lst.get(x));
            }
        }
        for (int x = 0; x < intSize ; x++) {
            lst.set(x,"");
        }
        for (int x = inSize - 1; x < size; x++) {
            String oldInfo = lst.get(x);
            list.add(oldInfo);
        }
        list.removeAll("",null);
    }
    return list; 
}

Try this approach: 试试这种方法:

public void duplicate(final List<String> inputList) {

        final List<String> temp = new ArrayList<>();
        inputList.forEach(element -> {
            temp.add(element);
            temp.add(element);
        });

        inputList.clear();
        inputList.addAll(temp);

}

Basically what I am doing here: another list called temp is used to store each element from your initial list 2 times. 基本上是我在这里做的事情:另一个名为temp列表用于将初始列表中的每个元素存储2次。 After that I just clean the initial list and add new stuff. 之后,我只是清理初始列表并添加新内容。

Instead of clear and addAll you can just return temp - it contains data as you need. 相反的clearaddAll你可以只返回temp -它包含的数据,因为你需要。 But don't forget to change method return type from void to List<String> in that case. 但是在这种情况下,请不要忘记将方法的返回类型从void更改为List<String>

Happy Coding :) 快乐编码:)

Try this 尝试这个

public static ArrayList<String> stutter(ArrayList<String> lst) {
        ArrayList<String> list = new ArrayList<String>();
        if (!lst.isEmpty()) {
            for (String string : lst) {
                list.add(string);
                list.add(string);
            }
        }
        return list;
    }
}

this could do what you need , its basically checks list if not empty and duplicate the strings in the list 这可以满足您的需要,它基本上检查列表是否不为空,并重复列表中的字符串

Following up on already good answers, here is my example with keeping the original sample method signature: 遵循已经很好的答案,这是我的示例,其中保留了原始的示例方法签名:

import java.util.List;
import java.util.ArrayList;
import java.util.Arrays;

public class Stutter {
    public static List<String> stutter(final List<String> words) {
        // Create the response array.
        List<String> response = new ArrayList<>();
        // Check for valid arguments. If not, return empty array of words.
        if (words == null || words.size() <= 0) {
            return response;
        }   
        // Iterate over the words that were passed in.
        for (final String word : words) {
            // Add the words twice to the response.
            response.add(word);
            response.add(word);
        }
        // The stutter response.
        return response;
    }
    public static void main(final String[] args) {
        final String[] myWords = { "hello", "world" };
        final List<String> myStutterWords = stutter(new ArrayList<>(Arrays.asList(myWords)));
        for (final String s : myStutterWords) {
            System.out.println(s);
        }
    }
}   

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

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