Search code examples
javalistarraylistjtextareatoarray

ArrayList cannot be cast to java.util.ArrayList, toArray returns nothing


I am trying to write a hierarchal block that puts elements into an ArrayList, all apart of a larger ArrayList. What the block does is take an existing input of text, and adds every line of text into elements of an ArrayList. Each Line is then created as an ArrayList of Strings, with each String being a word on that line (I used the String Split at spaces (" ") to perform this).

My problem is when trying to create this I needed to use the Arrays.asList (because a String Split returns a List)

Action Syllables = new AbstractAction("Syllables") {
    public void actionPerformed(ActionEvent e) {
        ArrayList<String> text = LinetoList(area);
        //"text" is a String ArrayList of every "line" in a piece of text
        //LinetoList is a method that returns an ArrayList based on each new line
        ArrayList<ArrayList> words = new ArrayList();
        for (int i = 0;  i < text.size(); i++) {
            ArrayList sentence =  (ArrayList) Arrays.asList(text.get(i).split(" "));
            /*Sentence is currently a list, however, changing the type to an Array 
             * or Arraylist Changes nothing */
            words.add(sentence);
            }
        for (int k = 0; k < words.size(); k++) {
            for (int i= 0; i < words.get(k).size(); i ++) {
                System.out.println(words.get(k).get(i));
            }
        }
    }
};

This was my original Method which return the error. I have since adjusted slightly, which no longer returns an error BUT, doesn't return anything.

Action Syllables = new AbstractAction("Syllables") {
    public void actionPerformed(ActionEvent e) {
        ArrayList<String> text = LinetoList(area);
        //"text" is a String ArrayList of every "line" in a piece of text
        //LinetoList is a method that returns an ArrayList based on each new "line"
        ArrayList<ArrayList> words = new ArrayList();
        for (int i = 0;  i < text.size(); i++) {
            ArrayList <String> sentences = new ArrayList();
            String sentence[] =  text.get(i).split(" ");
            sentence = sentences.toArray(sentence);
            /*Sentence is currently a list, however, changing the type to an Array 
             * or Arraylist Changes nothing */
            words.add(sentences);
            }
        if (words.size() ==  0)
        {System.out.println("Theres nothing here"); }
        else {
        for (int k = 0; k < words.size(); k++) {
            for (int i= 0; i < words.get(k).size(); i ++) {

                System.out.println(words.get(k).get(i));}

            }
        }
    }
};

Any feedback or ideas on how to approach a solution is greatly appreciated.

EDIT: Some people asked for the LinetoList function. A majority of the program uses ArrayLists of strings which is why it's so heavily used here.

private static ArrayList<String> LinetoList(JTextArea textArea) {

  String s[] = textArea.getText().split("\\r?\\n");
    ArrayList<String>arrList = new ArrayList<>(Arrays.asList(s)) ;
    return arrList;
}

Solution

  • So after taking a look at the comments on HOW the toArray and asList work, I tried a different approach that was based more on my linetolist function

    the result was

    Action Syllables = new AbstractAction("Syllables") {
        public void actionPerformed(ActionEvent e) {
            ArrayList<ArrayList> words = new ArrayList();
            ArrayList<ArrayList> splitLines = new ArrayList();
            ArrayList<String> characters = new ArrayList();
            //^^goes to (As hierarchey )
            ArrayList<String> text = LinetoList(area);
            //text is a String ArrayList of every line in a text
            //LinetoList is a method that returns an ArrayList based on each new line
            for (int i = 0;  i < text.size(); i++) {
                //for each line we have
                String sentence[] =  text.get(i).split(" ");
                ArrayList<String> splitText = new ArrayList<>(Arrays.asList(sentence));
                words.add(splitText);
                }
            for (int j = 0; j < words.size(); j++) {
                String sentence [] = text.get(j).split(" ");
                ArrayList<String> splitText = new ArrayList<>(Arrays.asList(sentence));
                ArrayList<ArrayList> SplitWords = new ArrayList();
                for (int i =0; i < sentence.length; i++) {
                    ArrayList<Character> SplitCharacters = new ArrayList<Character>();
                    for (int k = 0; k < splitText.get(i).length(); k ++) {
    
                    SplitCharacters.add(splitText.get(i).charAt(k));
    
                    }
                    SplitWords.add(SplitCharacters);
                }
                splitLines.add(SplitWords);
    
            if (words.size() ==  0)
            {System.out.println("Theres nothing here"); }
            else {
            for (int k = 0; k < words.size(); k++) {
                System.out.println("\n");
                for (int i= 0; i < words.get(k).size(); i ++) {
                    System.out.println(words.get(k).get(i));
                    }
    
                }
            System.out.println("That was the original method \n");
            for (int k = 0; k < splitLines.size(); k++) {
                System.out.println(splitLines.get(k).toString() + "\n");
    
                }
            }
        }       
    }
    };
    

    I included two different approaches to this problem, one that simplifies things as an ArrayList of words and one that gives an ArrayList of ArrayList of Characters (or a array list of words which hold letters) Thanks for all the help. Hopefully anyone else can take something away from my blunders.