user1323808
user1323808

Reputation: 31

Converting ArrayList Strings of to Characters?

So I have an arrayList of Strings imputed by the user.I also have a 2d array of chars : private char[][] puzzle ; I need help changing the Strings to characters so I can enter them into the 2d array can anyone help??

public class WordSearchPuzzle
{
    private char[][] puzzle ;
    private ArrayList<String> puzzleWords ;
    private int letterCount = 0 ;
    private int gridDimensions;

    public WordSearchPuzzle(ArrayList<String> userSpecifiedWords)
    {
        this.puzzleWords = userSpecifiedWords ;

    }

    private void createPuzzleGrid()
    {
        int i;
        for(i = 0; i < puzzleWords.size() ; i++){
            letterCount = puzzleWords.size() + letterCount ;
        }
        gridDimensions = letterCount * 2;
        puzzle = new char[gridDimensions][gridDimensions] ;
    }

Upvotes: 0

Views: 2179

Answers (3)

bpgergo
bpgergo

Reputation: 16037

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

public class WordSearchPuzzle {

    private char[][] puzzle ;
    private List<String> puzzleWords; //note: don't use a specific implementation (e.g. ArrayList) when you define a List  

    public WordSearchPuzzle(List<String> userSpecifiedWords)
    {
        this.puzzleWords = userSpecifiedWords ;

    }

    public void createPuzzleGrid()
    {
        puzzle = new char[puzzleWords.size()][];
        for(int i = 0; i < puzzleWords.size() ; i++){
            puzzle[i] = puzzleWords.get(i).toCharArray();
        }
    }   


    public void debugPuzzle(){
        System.out.println(Arrays.deepToString(puzzle));
    }
    /**
     * @param args
     */
    public static void main(String[] args) {
        List<String> userWords = new ArrayList<String>();
        userWords.add("word");
        userWords.add("longlongword");
        userWords.add("hello");

        WordSearchPuzzle puzzle = new WordSearchPuzzle(userWords);
        puzzle.createPuzzleGrid();
        puzzle.debugPuzzle();
    }

}

result

[[w, o, r, d], [l, o, n, g, l, o, n, g, w, o, r, d], [h, e, l, l, o]]

Upvotes: 0

maksimov
maksimov

Reputation: 5811

Tagged as homework. Like in your previous questions, I'll ask: did you try anything yet? How do you plan to solve this? What is your current problem?

I'll give you an idea. createPuzzleGrid() method inialises the puzzle array for you. Note how they calculate the dimensions from the list of words. This should point you in the right direction: how to copy characters from the list of words into the array.

Upvotes: 0

Aidanc
Aidanc

Reputation: 7011

Change each string to a charArray.

for(String item : ArrayList){
  item.toCharArray();
}

Upvotes: 1

Related Questions