【问题标题】:Return an array of Pairs which have the same length as the input array of Strings. (Java)返回一个与输入字符串数组长度相同的对数组。 (爪哇)
【发布时间】:2013-08-08 00:28:24
【问题描述】:

我想创建一个类,该类将返回一个与输入字符串数组长度相同的对数组。 此外,该对应具有字符串的首字母和字符串的长度。

例如; 创建(新字符串 [] {“线索”,“耶”,“霓虹灯”,“哈拉拉”}) 应该返回对数组 {['c',4],['y',3],['n',4],['h',6]}

所以,我的输入和输出都是数组。但是输出必须是一对的形式。这是我尝试过的:

import java.util.Arrays;



public class Couple {


public static Couple[] create(String[] source){

        for (int i = 0; i < source.length; i++) {

            System.out.print("["+","+source.length+"]") ;
        }
        return null;

    }            

    public static void main(String[] args) {
        System.out.println(Arrays.toString(create(new String[] {"clue", "yay", "neon", "halala"})));

    }

}

因为很明显有一些错误+我不希望它返回 null。但只是为了测试这段代码,我不得不这样做。 有任何想法吗?

【问题讨论】:

    标签: java


    【解决方案1】:
    public class Couple {
    
        private char firstChar;
        private int length;
    
        public Couple(char firstChar, int length) {
            this.length = length;
            this.firstChar = firstChar;
        }
    
        public static Couple[] create(String[] source) {
            Couple[] couples = new Couple[source.length]; // create the array to hold the return pairs
    
            for (int i = 0; i < source.length; i++) {
                String entry = source[i];
                if (entry != null) {
                    couples[i] = new Couple(entry.charAt(0), entry.length());
                } else {
                    // What do you want to do if there's a null value?
                    // Until you answer this we'll just leave the corresponding Couple null aswell
                }
            }
    
            return couples;
        }
    
        @Override
        public String toString() {
            return "Couple{" +
                    "firstChar=" + firstChar +
                    ", length=" + length +
                    '}';
        }
    }
    

    【讨论】:

    • 欢迎来到 Stack Overflow!由于这个问题可能是某人的作业(顺便说一句,这绝对没有错)社区成员更愿意帮助问题的作者了解如何做作业,而不是直接为他们做。这里的信念是,这对 OP 来说比给他们代码更长远的好处。感谢您的回答!
    • 我什至在要求其他人纠正我之前提供了我尝试过的代码:)
    • public String toString() { return ("["+"\'"+firstChar+"\'"+","+length+"]");我以这种方式编写了最后一点代码,因为我需要单引号中的第一个字符。 PS:我的代码在 cmets 中的显示方式与 Q 中的显示方式不同?原谅我...刚到论坛。非常感谢您的所有回答。它真的帮助了我:D
    【解决方案2】:

    你在正确的轨道上:

    • 与其返回null,不如创建一个对数组,并在循环中填充它。你知道结果的长度,因为你有源的长度
    • 要查看单个单词,请使用source[i]
    • 要删除单词的首字母,请使用source[i].charAt(0)
    • 要获取单词的长度,请使用source[i].length()
    • Couple 类需要两个数据成员 - charint;它们应该在构造函数中设置
    • 数据成员应该有 getter - public char getChar()public int getLength() 返回各自的成员
    • 打印应该在 main 中完成,在遍历返回的对数组的循环中

    你应该能够完成剩下的。

    【讨论】:

      【解决方案3】:

      你可能需要这个提示:

      Couple[] result = new Couple[source.length];
      

      并编写一个循环来创建Couple 实例并将它们放入result 数组中。

      【讨论】:

        【解决方案4】:

        使用此代码:

        import java.util.Arrays;
        
        public class Couple {
        
            public static String[] create(String[] source) {
        
                String[] temp = new String[source.length];
                for (int i = 0; i < source.length; i++) {
        
                    if (source[i].length() > 0) {
                        String newString = "[" + source[i].charAt(0) + ","
                                + source.length + "]";
                        //System.out.print(newString);
                        temp[i] = newString;
                    } else {
                        String newString = "[" + " " + "," + 0 + "]";
                        //System.out.print(newString);
                        temp[i] = newString;
                    }
        
                }
                return temp;
        
            }
        
            public static void main(String[] args) {
                System.out.println(Arrays.toString(create(new String[] { "clue", "yay",
                        "neon", "halala"})));
        
            }
        
        }
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 2021-03-05
          • 1970-01-01
          • 2019-01-28
          • 1970-01-01
          • 2013-11-29
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多