我的代碼遇到了一個問題,我無法刪除數(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;}
如何從此代碼中的數(shù)組列表中刪除空元素
慕碼人2483693
2022-09-14 10:49:57