简体   繁体   English

我如何转换 arraylist<string> 至 arraylist<integer></integer></string>

[英]How can I convert arraylist <String> to arraylist <integer>

My goal is to find on which specific index is String from ArrayList and add them to new ArrayList, So if house is [0] than i want to return new ArrayList with integer.我的目标是找到哪个特定索引是 ArrayList 中的字符串,并将它们添加到新的 ArrayList,所以如果房子是 [0],我想用 integer 返回新的 ArrayList。

public class List {
public static void main(String[] List) {
    List<String> words = new ArrayList<>();
    words.addAll(Arrays.asList("house", "bike","dog","house","house"));
    System.out.println(getIntegerArray(words,house));


 public static List<Integer> getIntegerArray(List<String> words, String word) {
        List<Integer> numbers = new ArrayList<>();
        for (int i = 0; i < numbers.size() ; i++) {

        }

At the begging I have ArrayList like this Input:一开始我有这样的 ArrayList 输入:

["house", "bike","dog"]

And I want to get new ArrayList like this Output:我想像这样获得新的 ArrayList Output:

[0,1,2]

You can do it just by checking if the string passed to the method is contained in the list and adding the number to the List numbers .您可以通过检查传递给该方法的字符串是否包含在列表中并将数字添加到 List numbers来完成。

Here an example of the method getIntegerArray :这是方法getIntegerArray的示例:

public static List<Integer> getIntegerArray(List<String> words, String word) {
    List<Integer> numbers = new ArrayList<>();

    for (int i=0; i < words.size() ; i++) {
        if (word.equals(words.get(i)))
            numbers.add(i);
    }
    return numbers;
}

PS: in System.out.println(getIntegerArray(words, house)); PS: 在System.out.println(getIntegerArray(words, house)); you are passing a variable house which is not declared.您正在传递一个未声明的可变house Probably you wanted to write "house" .可能您想写"house"

You should not worry about the size of numbers list, just add appropriate indexes as needed:您不必担心numbers列表的大小,只需根据需要添加适当的索引即可:

public static List<Integer> getIntegerArray(List<String> words, String word) {
    List<Integer> numbers = new ArrayList<>();
    for (int i = 0, n = words.size(); i < n; i++) {
        if (Objects.equals(word, words.get(i)))
            numbers.add(i);
    }
    return numbers;
}

Or use Stream API to get the list of indexes:或者使用 Stream API 获取索引列表:

public static List<Integer> getIntegerArray(List<String> words, String word) {
    return IntStream.range(0, words.size())
        .filter(i -> Objects.equals(word, words.get(i)))
        .boxed()
        .collect(Collectors.toList());
}

getIntegerArray(List<String> words, String word) with a single word can't get you a list of integers with more than one element, I suggest passing the entire String[] , called findwords in the code below.带有单个wordgetIntegerArray(List<String> words, String word)无法为您提供包含多个元素的整数列表,我建议传递整个String[] ,在下面的代码中称为findwords Otherwise, lists have a indexOf() , which makes things really simple:否则,列表有一个indexOf() ,这让事情变得非常简单:

public static void main (String[] args) {
    List<String> words = new ArrayList<>();
    words.addAll(Arrays.asList("house", "bike","dog","house","house"));
    System.out.println(getIntegerArray(words,args));
}

public static List<Integer> getIntegerArray(List<String> words, String[] findwords) {
    List<Integer> numbers = new ArrayList<>();
    for(String word: findwords)
        numbers.add(words.indexOf(word));
    return numbers;
}

This will produce [0, 1, 2] as output when run with command line arguments house bike dog .当使用命令行 arguments house bike dog运行时,这将生成[0, 1, 2]作为 output 。 See https://ideone.com/gYefsa (ideone doesn't support passing command line arguments, so args is just initialized inside the code).参见https://ideone.com/gYefsa (ideone不支持传递命令行arguments,所以args只是在代码内部初始化)。

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

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