简体   繁体   中英

input data in an arraylist without using an array in java

i am not very good in java and i am trying to make my codes better.i am trying to enter a sentence then split the sentence into word and then store the words in an arraylist.Afterwards i have to enter a word and check if the word is found into the sentence.So far i have managed to do and the codes work.however i want to do it without using an array since i dont ever use the array afterwards,so it is kind of redundant.Is there a way so as i may enter the words in an arraylist directly without using an array?

  here are my codes:
   public static void main(String[] args){
    //the user is asked to enter a sentence
     Scanner input=new Scanner(System.in);
     System.out.println("enter a sentence");
        String text=input.nextLine();

        //the sentence is split
        String[] s=text.split("[[\\s+]*|[,]*|[\\.]]");

         ArrayList<String> list=new ArrayList<String>();
          //the word are stored into a variable then added into the array
            for(String ss:s){
                list.add(ss);
                }

          System.out.println("enter a word");
          String word=input.next();
            //check if the word is in the arraylist
           for(int i=0;i<list.size();i++){
            if(word.equals(list.get(i))){
              System.out.println("the word is found in the sentence");
                  System.exit(0);
          }

        }
        System.out.println("the word is not found in the sentence");

      }

An option would be to loop over all element in the array returned by text.split(). So:

ArrayList<String> list = new ArrayList<String>();
for(String s : text.split("[[\\s+]*|[,]*|[\\.]]")){
    list.add(s);
}

The array that you create when you split the string is necessary , however you can create the ArrayList with one line of code, this will allow the array to be disposed of whenever the Garbage Collector runs.

ArrayList<String> list = new ArrayList<Element>(Arrays.asList(text.split("[[\\s+]*|[,]*|[\\.]]")));

However its important to note, that text.split is still creating an array.

Use Arrays.asList(..) to create a list from the splitted input string.

String[] s=text.split("[[\\s+]*|[,]*|[\\.]]");
List<String> list = Arrays.asList(s);

then just use List.contains(Object) to check if the entered word is contained.

if(list.contains(word)){
   ...
}

Thus the final programm will look like this

public static void main(String[] args) {
    // the user is asked to enter a sentence
    Scanner input = new Scanner(System.in);

    System.out.println("enter a sentence");
    String text = input.nextLine();

    // the sentence is split
    String[] splittedSentence = text.split("[[\\s+]*|[,]*|[\\.]]");

    List<String> words = Arrays.asList(splittedSentence); // the word are stored into a variable then added into the array

    System.out.println("enter a word");
    String word = input.next();

    // check if the word is in the list
    if (words.contains(word)) {
        System.out.println("the word is found in the sentence");
    } else {
        System.out.println("the word is not found in the sentence");
    }
}

如果您真的想StringTokenizer数组StringTokenizer ,请看一下StringTokenizer

new StringTokenizer(text, ", .\t\n\r");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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