Java 限制字符串数组或向其添加额外符号
我想创建一个程序,在给定一对字符串的情况下,根据该对中提供的长度限制它们或添加额外字符。例如,它应该表现为:Java 限制字符串数组或向其添加额外符号,java,arrays,string,Java,Arrays,String,我想创建一个程序,在给定一对字符串的情况下,根据该对中提供的长度限制它们或添加额外字符。例如,它应该表现为: create (new Line[] {"try","chicken"}, new Couple[] {new Couple(’t’,3),new Couple(’c’,3)}) 应该回来 {"try", "chi"} 当长度(指定的数字)小于或等于给定的单词时,出现上述情况。 下面是一个长度大于单词的示例 create (new Line[] {"try","chicken"}
create (new Line[] {"try","chicken"},
new Couple[] {new Couple(’t’,3),new Couple(’c’,3)})
应该回来
{"try", "chi"}
当长度(指定的数字)小于或等于给定的单词时,出现上述情况。
下面是一个长度大于单词的示例
create (new Line[] {"try","chicken"},
new Couple[] {new Couple(’t’,3),new Couple(’c’,9)})
应该回来
{"try", "chickenPP"}
也就是说,我们用字母p替换剩余的空格
以下是我尝试过的:
import java.util.Arrays;
public class Learn {
private char firstChar;
private int length;
private char chars;
private char FILL_CHARACTER = 'P';
public Learn(char firstChar, int length, char chars) {
this.length = length;
this.firstChar = firstChar;
this.chars = chars;
}
public static String[] create(String[] first, Couple[] second) {
String[] strings = new String[first.length];
for (int i = 0; i < first.length; i++) {
if (second.length = first.chars) {
learns[i] = first[i];
} else if (second.length > first.chars) {
learns[i] = first[i] + FILL_CHARACTER;
}
}
}
}
导入java.util.array;
公共课堂学习{
私有字符firstChar;
私有整数长度;
私有字符;
私有字符FILL_CHARACTER='P';
公共学习(char firstChar、int-length、char-chars){
这个长度=长度;
this.firstChar=firstChar;
this.chars=chars;
}
公共静态字符串[]创建(首先是字符串[],其次是成对字符串[]){
String[]strings=新字符串[first.length];
for(int i=0;ifirst.chars){
学习[i]=第一个[i]+填充字符;
}
}
}
}
正如你所看到的,我有很多错误。我想我可能对API感到困惑了。请帮我更正。我不确定你在用
学习和情侣做什么,但你可以通过字符串
来完成:
public static String[] create(String[] words,int[] lengths) {
char fill = 'P';
String[] ret = new String[words.length];
for ( int i = 0 ; i < ret.length ; i++ ) {
ret[i] = words[i];
while ( ret[i].length() < lengths[i] ) {
ret[i] += fill;
}
ret[i] = ret[i].substring(0,lengths[i]);
}
return ret;
}
publicstaticstring[]create(String[]words,int[]length){
字符填充='P';
String[]ret=新字符串[words.length];
对于(int i=0;i
用法:
create(新字符串[]{“try”,“chicken”},新int[]{3,3})
所以行
实际上是字符串
,而对
和整数长度一起是一个被忽略的字符,学习
也是字符串
?你想用这些额外的课程来完成什么?我已经编辑了上面的帖子。它有用吗(什么是班对?为什么你有一个不用的班学习?