Java 按长度筛选数组元素
因此,我希望能够要求用户输入数组的长度并用名称填充它。执行此操作后,程序将打印出名称及其旁边字符串的长度Java 按长度筛选数组元素,java,arrays,methods,Java,Arrays,Methods,因此,我希望能够要求用户输入数组的长度并用名称填充它。执行此操作后,程序将打印出名称及其旁边字符串的长度 public static void main(String[] args) { Scanner sc = new Scanner(System.in); String names []; // Read the number of kids System.out.print("How many kids are there? "); int siz
public static void main(String[] args)
{
Scanner sc = new Scanner(System.in);
String names [];
// Read the number of kids
System.out.print("How many kids are there? ");
int size = sc.nextInt();
// Read the names of the kids
String [] kids = new String [size];
for (int k = 0; k < kids.length; k++)
{
System.out.print("Enter name of kid #" + k + ": ");
kids[k] = sc.next();
}
// Print all the names of the kids and the length of their names
System.out.println("Names of kids with lengths are: ");
for (int i = 0; i < kids.length; i++)
{
System.out.println(kids[i] + " " + kids[i].length());
}
publicstaticvoidmain(字符串[]args)
{
扫描仪sc=新的扫描仪(System.in);
字符串名[];
//看看孩子的数量
System.out.print(“有多少孩子?”);
int size=sc.nextInt();
//读孩子们的名字
String[]kids=新字符串[大小];
for(int k=0;k
在此之后,我希望用户输入一个整数,程序将在数组中搜索该长度的任何字符串,如果它找到任何字符串,我希望它打印出它找到的所有字符串的首字母缩写。我必须使用该程序的方法。但我似乎无法理解这个问题
static int countByLength(String [] names, int length)
{
int count = 0;
return count;
}
/**
* Filter an array of names and keep only those names with a given length
* @param names: an array of names
* @param length: an integer length
* @return the array of names that have the given length
*/
static String [] filterByLength(String [] names, int length)
{
String [] filtered = new String[length];
for(int k = 0; k < filtered.length; k++)
{
filtered [k] =
}
return filtered;
}
静态int countByLength(字符串[]名称,int长度)
{
整数计数=0;
返回计数;
}
/**
*筛选名称数组并仅保留具有给定长度的名称
*@param names:名称数组
*@param length:一个整数长度
*@返回具有给定长度的名称数组
*/
静态字符串[]filterByLength(字符串[]名称,整数长度)
{
字符串[]已筛选=新字符串[长度];
for(int k=0;k
}一个问题是,您不知道将得到多少结果,因此数组不是最好的数据结构。我建议改为使用列表,例如ArrayList。然后,您可以迭代数组,将所有匹配的字符串添加到列表中
static List<String> filterByLength(String [] names, int length)
{
List<String> filtered = new ArrayList<String>();
for(int k = 0; k < filtered.length; k++)
{
if (names.length() == length) filtered.add(names[k]);
}
return filtered;
}
静态列表过滤器长度(字符串[]名称,整数长度)
{
List filtered=new ArrayList();
for(int k=0;k
变量(参数)名称的错误选择增加了您的困惑。countByLength
中的length
参数是要查找的名称的长度,而filterbyllength
中的length
也是如此。然而,您使用filterbyleength
中的length
作为名称数组的长度,换句话说,就是名称的总数
如果你真的想使用一个普通数组(我建议在评论中注意@Jack的建议,或者编辑@Leander的答案),可能最简单的方法是先计算匹配的名称数,然后创建一个新的名称数组,并用这么多的元素填充它。看起来您想通过简单地获取过滤数组的大小来进行计数,这就是为什么这已经成为一个鸡和蛋的问题。我将使用您创建的方法来回答这个问题,忽略其有效或无效的解决方案
// Count the array that have name with same length
static int countByLength(String [] names, int length)
{
int count = 0;
for(int i = 0;i < names.length;i++)
{
if(names[i].length() = length) count++;
}
return count;
}
/**
* Filter an array of names and keep only those names with a given length
* @param names: an array of names
* @param length: an integer length
* @return the array of names that have the given length
*/
static String [] filterByLength(String [] names, int length)
{
String [] filtered = new String[countByLength(names, length)];
int index = 0;
for(int k = 0; k < filtered.length; k++)
{
if(names[k].length() == length) filtered[index++]=names[k];
}
return filtered;
}
//计算具有相同长度名称的数组
静态int countByLength(字符串[]名称,int长度)
{
整数计数=0;
for(int i=0;i
如果您使用Java 8并使用列表而不是数组,则可以使用过滤器
在一行代码中实现这一点:
List<String> filterByLength(List<String> names, int length) {
return names.stream().filter(n -> n.length() == length).collect(Collectors.toList());
}
List filterByLength(列表名称,整数长度){
返回names.stream().filter(n->n.length()==length.collect(Collectors.toList());
}
将names.stream()
转换为列表
流
只保留流中长度等于filter(n->n.length()==length)
的元素length
从collect(Collectors.toList())
流返回
列表
ArrayList
并在之后将其转换为数组。是的,我粘贴了讲师发布的程序的原始版本。我继续在我的程序中重命名它们,因为我感到困惑。谢谢。我知道这不是编写这个程序的最佳方式,但是我必须学习基础知识,这就是我必须编写的代码