简体   繁体   English

在数组列表中输入数据而不在Java中使用数组

[英]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? 我在Java中不是很好,并且我想使我的代码更好。我正在尝试输入一个句子,然后将该句子拆分为单词,然后将单词存储在arraylist中。然后我必须输入单词并检查是否句子中找到了单词。到目前为止,我已经成功地做到了,代码也起作用了。但是,我以后不使用数组,所以我不想使用数组,所以这是多余的。有没有办法因为我可以不使用数组直接在arraylist中输入单词?

  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(). 一种选择是遍历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,这将允许在运行垃圾收集器时将其丢弃。

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

However its important to note, that text.split is still creating an array. 但是需要注意的重要一点是,text.split仍在创建数组。

Use Arrays.asList(..) to create a list from the splitted input string. 使用Arrays.asList(..)Arrays.asList(..)后的输入字符串创建列表。

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. 然后只需使用List.contains(Object)来检查是否包含输入的单词。

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");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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