簡體   English   中英

拆分整數數組

[英]Splitting an array of integers

我有這個小問題,因為它生成了 java.util.NoSuchElementException 我無法找到似乎是什么問題。 任何人都可以指出似乎是錯誤的地方,請這里是代碼,

import java.util.Collections;
import java.util.Vector;


public class Splitting {

    /**
     * @param args
     */

    protected int [] temp;
    Vector<Integer> vec = new Vector<Integer>();

    public void split(String input)
    {
        if (input == null)
        {
        String[] str;
        str = input.split(",");
        temp = new int[str.length];

        System.out.println(str);

            for (int i = 0; i < str.length; i++)
            {

                temp[i] = Integer.parseInt(str[i]);
                vec.add(temp[i]);

            }
        }
        System.out.println(vec);
        Collections.sort(vec);


        System.out.println(vec);
        Collections.max(vec);
    }



    public static void main(String[] args) {
        // TODO Auto-generated method stub

        Splitting obj = new Splitting();

        obj.split("12,65,21,23,89,67,12");



    }

}

可能應該是

if (input != null)

您可以使用以下代碼段簡單地將數組轉換為向量:

vec = new Vector(Arrays.asList(str));

可能,它不適用於您的情況(因為您需要將字符串解析為整數),但將來很高興知道。 謝謝

你有

if (input == null)

你的意思是有

if (input != null)

?

可能是if (input == null)應該是if (input != null)

使用番石榴

String input = "Some very stupid data with ids of invoces like 121432, 3436534 and 8989898 inside";
List<String> l =Lists.newArrayList(Splitter.on(" ").split(input));
Collection<Integer> c = Collections2.transform(l, new Function<String, Integer>(){
    @Override
    public Integer apply(String s) {
        return Integer.parseInt(s);
    }});
List<Integer> l2 = Lists.newArrayList(c);

Collections.sort(l2);
Collections.max(l2);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM