第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何從此代碼中的數(shù)組列表中刪除空元素

如何從此代碼中的數(shù)組列表中刪除空元素

慕碼人2483693 2022-09-14 10:49:57
我的代碼遇到了一個問題,我無法刪除數(shù)組列表中的空元素。它最終返回,但是這些空元素在多次嘗試后不會被刪除[1, 2, 3, 0, 8,  , 12 ,  ,   34 , 0 ]public static ArrayList<String> readNumbers() {    Scanner inFile = null;    File file = null;    String filePath = (JOptionPane.showInputDialog("Please enter a file path"));    int size = 0;    ArrayList<String> result = new ArrayList<String>();    try {        file = new File(filePath);        inFile = new Scanner(file);        int skippedCounter = 0;        for(int i = 0; inFile.hasNext(); i++){            if(inFile.hasNextInt())                result.add(inFile.next());            else{                String strOut = "";                String data = inFile.nextLine();                for(int j = 0; j <= data.length() - 1; j++){                    if(!Character.isLetter(data.charAt(j))){                        strOut += data.charAt(j);                    }                    else                        skippedCounter++;                }                if(!strOut.isEmpty())                    result.add(strOut);            }        }    }     catch (FileNotFoundException e) {        System.out.println("File not found");    }     catch(NumberFormatException e){        System.out.println("Not a number");    }    finally {        inFile.close();    }    int count = 0;    result.removeIf(String::isEmpty);    return result;}
查看完整描述

3 回答

?
三國紛爭

TA貢獻1804條經(jīng)驗 獲得超7個贊

String::isEmpty()僅檢查字符串的長度是否為零。如果您的字符串僅由空格或其他空格組成,則不會返回。true

您可以先執(zhí)行 ,然后檢查 。String.trim()String.isEmpty()

result.removeIf(s -> s.trim().isEmpty());


查看完整回答
反對 回復 2022-09-14
?
米脂

TA貢獻1836條經(jīng)驗 獲得超3個贊

我相信在Java 8及更高版本中,你可以做這樣的事情從列表中刪除所有空值:results.removeIf(item -> != StringUtils.isEmpty(item));



查看完整回答
反對 回復 2022-09-14
?
天涯盡頭無女友

TA貢獻1831條經(jīng)驗 獲得超9個贊

您可以嘗試:

List<Integer> listWithNulls = Arrays.asList(1,null, 2, 3, null, 4);

List<Integer> listWithoutNulls = listWithNulls.stream().filter(Objects::nonNull).collect(Collectors.toList());



查看完整回答
反對 回復 2022-09-14
  • 3 回答
  • 0 關注
  • 158 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網(wǎng)微信公眾號