Java 返回与输入字符串数组长度相同的对数组。(爪哇)
我想创建一个类,该类将返回与字符串输入数组长度相同的成对数组。 此外,该对应该具有字符串的第一个字母和字符串的长度 比如,; 创建(新字符串[]{“线索”、“耶”、“霓虹灯”、“哈拉拉”}) 应该返回对的数组 {['c',4],'y',3],'n',4],'h',6]} 所以,我的输入和输出都是数组。但是输出必须是成对的。以下是我尝试过的:Java 返回与输入字符串数组长度相同的对数组。(爪哇),java,Java,我想创建一个类,该类将返回与字符串输入数组长度相同的成对数组。 此外,该对应该具有字符串的第一个字母和字符串的长度 比如,; 创建(新字符串[]{“线索”、“耶”、“霓虹灯”、“哈拉拉”}) 应该返回对的数组 {['c',4],'y',3],'n',4],'h',6]} 所以,我的输入和输出都是数组。但是输出必须是成对的。以下是我尝试过的: import java.util.Arrays; public class Couple { public static Couple[] cre
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"})));
}
}
导入java.util.array;
公务舱夫妇{
公共静态耦合[]创建(字符串[]源){
for(int i=0;i
很明显有一些错误,我不希望它返回null。但是为了测试这段代码,我不得不这么做。
有什么想法吗 您可能需要的提示如下:
Couple[] result = new Couple[source.length];
然后编写一个循环,创建
耦合
实例,并将它们放入结果
数组中。您的思路正确:
- 与其返回
,不如创建一个对数组,并将其填充到循环中。您知道结果的长度,因为您知道源的长度null
- 要查看单个单词,请使用
source[i]
- 要切掉单词的首字母,请使用
source[i].charAt(0)
- 要获取单词的长度,请使用
source[i].length()
类需要两个数据成员—一个Couple
和一个char
;它们应该在构造函数中设置int
- 数据成员应该有getter-
和public char getChar()
返回各自的成员public int getLength()
- 打印应该在
中完成,在循环中遍历返回的成对数组main
公共类{
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 +
'}';
}
}
私有字符firstChar;
私有整数长度;
公共耦合(char firstChar,int-length){
这个长度=长度;
this.firstChar=firstChar;
}
公共静态耦合[]创建(字符串[]源){
Couple[]couples=new Couple[source.length];//创建数组以保存返回对
for(int i=0;i
使用以下代码:
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"})));
}
}
导入java.util.array;
公务舱夫妇{
公共静态字符串[]创建(字符串[]源){
String[]temp=新字符串[source.length];
for(int i=0;i0){
字符串newString=“[”+源[i]。字符(0)+“
+source.length+“]”;
//系统输出打印(新闻字符串);
temp[i]=新闻字符串;
}否则{
字符串newString=“[”+”+“+”,“+0+”];
//系统输出打印(新闻字符串);
temp[i]=新闻字符串;
}
}
返回温度;
}
公共静态void main(字符串[]args){
System.out.println(Arrays.toString(创建(新字符串[]{“线索”,“耶”,
“霓虹灯”、“哈拉拉”});
}
}
欢迎使用堆栈溢出!由于这个问题可能是某人的家庭作业(顺便说一句,这绝对没有错),社区成员更喜欢帮助问题的作者理解如何做家庭作业,而不是直接为他们做。这里的信念是,这对老年退休金计划比给他们编码更有长期的好处。谢谢你的回答!我甚至在要求别人纠正我之前提供了我尝试过的代码:)public String to String(){return(“[”+“\”+“+”+“\””+“\”””+“+”,“+length+“]”;}我以这种方式编写代码的最后一位,因为我需要单引号中的第一个字符。PS:我的代码并不像在Q中那样出现在注释中?请原谅我…刚到论坛。非常感谢你的回答。它真的帮助了我:D