【问题标题】:Converting ArrayList Strings of to Characters?将 ArrayList 字符串转换为字符?
【发布时间】:2012-04-28 17:26:39
【问题描述】:

所以我有一个由用户估算的字符串数组列表。我还有一个二维字符数组:private char[][] puzzle;我需要帮助将字符串更改为字符,以便我可以将它们输入到二维数组中,有人可以帮忙吗??

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] ;
    }

【问题讨论】:

    标签: java string arraylist char multidimensional-array


    【解决方案1】:
    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();
        }
    
    }
    

    结果

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

    【讨论】:

    • 这会对二维数组的大小产生影响。我想将它保持在 arrayList * 2 中字符的长度,所以这是我的代码:private void createPuzzleGrid() { int i, itemLength;字符串项; for (i = 0; i
    • 好的,我明白,这个结果不适合你:[[w, o, r, d], [l, o, n, g, l, o, n, g, w, o, r, d], [h, e, l, l, o]]。但我仍然不明白你想要达到什么目的?您使用puzzle[][] 的目的是什么?我不确定char[letterCount * 2][letterCount * 2] 是否有用,也许您需要char[maxWordLenght][maxWordLenght]
    • 感谢您的回复好吧,我试图实现的是一个使用二维数组的单词搜索难题,其中用户输入一个字符串数组列表,我们必须将字符串转换为字符,因为二维数组只会接受字符,然后将单词(作为字符)随机输入二维数组。感谢一百万的帮助!如果您有 Skype,您可以更轻松地添加我聊天!
    • 我明白了。您将如何定义二维数组的大小,您将尝试在其中随机放置单词? (我的意思是通俗地说)
    【解决方案2】:

    标记为作业。就像你之前的问题一样,我会问:你尝试过什么了吗?你打算如何解决这个问题?您目前的问题是什么?

    我会给你一个想法。 createPuzzleGrid() 方法为您初始化拼图数组。请注意他们如何从单词列表中计算维度。这应该为您指明正确的方向:如何将字符从单词列表复制到数组中。

    【讨论】:

      【解决方案3】:

      将每个字符串更改为charArray

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

      【讨论】:

      • 将字符数组放入数组或arrayList
      • @user1323808 如果您查看文档说明Returns: a newly allocated character array whose length is the length of this string and whose contents are initialized to contain the character sequence represented by this string.
      猜你喜欢
      • 2011-09-13
      • 2019-09-04
      • 2015-07-22
      • 2014-09-19
      • 2014-05-16
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-11-12
      相关资源
      最近更新 更多