数组包含空值

时间:2018-05-05 16:03:36

标签: java

我试图从文本文件中读取一些单词,然后将它们排列成降序数组。 我达到了成功读取单词的地步,将单词存储在数组中,当我继续将其排列为降序形式时,我注意到我的数组也包含一些空值。 当我尝试使用(下面的完整代码)删除空值     <?php if(!empty($work->video_url)): ?> <iframe ... ></iframe> <?php endif; ?> ,它仍然没有奏效。 经过一段时间的努力,我觉得我需要一些帮助。 此阶段的代码,文本文件和输出如下:     `

Arrays.stream(w.words).filter(x->x != null).toArray();

file.txt是:

import java.io.BufferedReader; import java.io.File; import java.io.FileNotFoundException; import java.io.FileReader; import java.io.IOException; import java.util.ArrayList; import java.util.Arrays; import java.util.Collections; import java.util.Comparator; import java.util.HashMap; import java.util.HashSet; import java.util.LinkedHashMap; import java.util.Map; import java.util.Objects; import java.util.Random; import java.util.Scanner; import java.util.Set; import java.util.stream.Collectors; public class Practise { private Scanner fp; private Scanner scanLine; private String[] words = new String[6] ; int count; String temp; String [][] samewords; int size; String[] words_sorted; public Practise() { openFile(); readFile(); printa(); } private void openFile() { try { fp = new Scanner(new File ("D:\\workspace\\file.txt")); } catch (Exception e) { System.out.println("File does not exist"); } } private void readFile() { try { count = 0; while (fp.hasNextLine()) { String strLine = fp.nextLine(); scanLine = new Scanner(strLine); words[count] = scanLine.next(); System.out.println("Here2"+words[count]); System.out.println(); count++; } } catch (Exception e) { System.err.print("Error: " + e.getMessage()); } } private void printa() { try (BufferedReader br = new BufferedReader(new FileReader("D:\\workspace\\file.txt"))) { size = findLongestWords(); samewords = new String[size][size]; String line; int i = 0; String [] temp; while ((line = br.readLine()) != null) { temp = line.split(","); for (int j = 0; j < samewords[i].length; j++) { samewords[i][j] = temp[j]; System.out.println(samewords[i][j]); } i++; } //System.out.println(answers[1][2]); } catch (IOException e) { e.printStackTrace(); } } public int findLongestWords() throws FileNotFoundException { int longest_word = 0; int current; BufferedReader sc = new BufferedReader(new FileReader("D:\\workspace\\file.txt")); String li; String[] tr; try { while ((li = sc.readLine())!= null ) { tr = li.split(","); current = tr.length; if (current > longest_word) { longest_word = current; } } } catch (IOException e) { // TODO Auto-generated catch block e.printStackTrace(); } System.out.println("\n"+longest_word+"\n"); return longest_word; } private String[] sort(String[] string) { /*Local variables*/ Map<String, Integer> map=new LinkedHashMap<String, Integer>(); Map<String, Integer> mapCopy=new LinkedHashMap<String, Integer>(); int [] lengthsArray=new int[string.length]; String [] sortedStrings=new String[string.length]; int counter1=0; int counter2=0; /* Store all the pairs <key,value> * i.e <string[i],string[i].length> */ for(String s:string) { System.out.println(s); map.put((String) s, s.length()); lengthsArray[counter1]= s.length();//store all the lengths counter1++; } mapCopy=new LinkedHashMap<String, Integer>(map);//make a copy of map Arrays.sort(lengthsArray);//sort the array of lengths /* * Sort array according to the array of lengths * by finding the matching value from the map * then add it to the final string array,and then remove it from the map */ for(int item:lengthsArray) { for(Map.Entry<String, Integer> e:map.entrySet()) { if(item==e.getValue()) { sortedStrings[counter2]=e.getKey(); counter2++; map.remove(e.getKey()); break; } } } map=mapCopy; System.out.println(map);//print map return sortedStrings; } public static void main(String[] args) { Practise w = new Practise(); System.out.println(Arrays.toString(w.words)); w.sort(w.words); } } `

输出是:

ACT,CAT,AT,RAT,PAT,TAT

1 个答案:

答案 0 :(得分:1)

null是因为您声明的单词数组(预定义大小)。可能你可以改变它并使用ArrayList(因为它可以是动态大小)而不是String数组,这可以帮助你解决。只是为了帮助您,请按照以下更改:

  1. private List<String> words = new ArrayList<>();
  2. /*update below line in readFile() instead of words[count] = scanLine.next();*/ words.add(scanLine.next());
  3. Change method signature sort(List<String> string)
  4. //also update below declarations int[] lengthsArray = new int[string.size()]; String[] sortedStrings = new String[string.size()];
  5. change Arrays.toString(w.words) to just w.words in print statement
  6. 希望这会有所帮助。一切都很好看。