[英]How can I make the Array List be called in another class java
I need to write a code that checks all the words in an array list and tells me how many of these have a certain length.我需要编写一个代码来检查数组列表中的所有单词并告诉我其中有多少具有一定的长度。 I understand how to do that, but I can't figure out how to make the
ArrayList
to be read in the second class so I can apply it in the program.我了解如何做到这一点,但我不知道如何使
ArrayList
在第二个 class 中读取,因此我可以在程序中应用它。
MAIN主要的
import java.util.Scanner;
class Main {
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
java.util.ArrayList myList = new java.util.ArrayList();
myList.add("cat");
myList.add("mouse");
myList.add("frog");
myList.add("dog");
myList.add("dog");
int len = in.nextInt();
WordList wl = new WordList();
wl.numWordsOfLength(len);
}
}
SECOND CLASS第二个CLASS
public class WordList{
public int numWordsOfLength(int len){
int count = 0;
for(int i=0;i<len;i++){
if(((String)myList.get(i)).length()==len){
count++;
}
}
return count;
}
}
You need to pass the list as parameter:您需要将列表作为参数传递:
public int numWordsOfLength(int len, List<String> myList){
int count = 0;
for(int i=0;i<myList.size;i++){
if((myList.get(i)).length()==len){
count++;
}
}
return count;
}
or via the WordList
Constructor:或通过
WordList
构造函数:
import java.util.List;
public class WordList{
private final List<String> myList;
WordList(List<String> myList){
this.myList = myList;
}
public int numWordsOfLength(int len){
int count = 0;
for (String s : myList) {
if (s.length() == len) {
count++;
}
}
return count;
}
}
Side node instead of:侧节点而不是:
java.util.ArrayList myList = new java.util.ArrayList();
just do:做就是了:
import java.util.List;
....
List<String> myList = new ArrayList();
Full Running example:完整运行示例:
public class Test {
public static class WordList{
private final List<String> myList;
WordList(List<String> myList){
this.myList = myList;
}
public int numWordsOfLength(int len){
int count = 0;
for (String s : myList) {
if (s.length() == len) {
count++;
}
}
return count;
}
}
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
List<String> myList = new ArrayList<>();
myList.add("cat");
myList.add("mouse");
myList.add("frog");
myList.add("dog");
myList.add("dog");
int len = in.nextInt();
WordList wl = new WordList(myList);
System.out.println(wl.numWordsOfLength(len));
}
}
This depends on your interpretation of "read in the second class".这取决于您对“第二课阅读”的解释。
My guess is you want the second class to have "myList" as a private variable, so the code will work when you call the numWordsOfLength() function.我的猜测是您希望第二个 class 将“myList”作为私有变量,因此当您调用 numWordsOfLength() function 时代码将起作用。
In order to do this, you should pass the array you built to WordList as a dependency.为此,您应该将构建的数组作为依赖项传递给 WordList。 There are generally two ways to give an object a dependency.
通常有两种方法可以给 object 一个依赖项。 Either when you create the object (constructor based) or later by calling a method (setters).
当您创建 object(基于构造函数)或稍后通过调用方法(设置器)时。
So in main, your goal would be to do either:因此,总的来说,您的目标是执行以下任一操作:
WordList wl = new WordList();
wl.setList(myList)
wl.numWordsOfLength(len);
Or:或者:
WordList wl = new WordList(myList);
wl.numWordsOfLength(len);
Either way, you will have to add "myList" variable inside the WordList and then expose it either via a separate setter method (setList) or in the constructor itself.无论哪种方式,您都必须在 WordList 中添加“myList”变量,然后通过单独的 setter 方法 (setList) 或在构造函数本身中公开它。
I'd prefer a constructor in this case, since the name of the class "WordList" implies that it IS a list of things and therefore should encapsulate the thing it is.在这种情况下,我更喜欢构造函数,因为 class “WordList”的名称意味着它是一个事物列表,因此应该封装它。
ie IE
public class WordList{
private List<String> myList;
public WordList(List<String> listOfWords) {
this.myList = listOfWords;
}
public int numWordsOfLength....
}
You don't need that second class at all, if you use Java 8+:如果您使用 Java 8+,则根本不需要第二个 class:
...
java.util.ArrayList<String> myList = new java.util.ArrayList();
myList.add("cat");
myList.add("mouse");
myList.add("frog");
myList.add("dog");
myList.add("dog");
int len = in.nextInt();
int count = myList.stream().map(String::length).filter(c->c==len).count(); <--that's your answer
...
If you prefer to use that second class, you can just use this "count" expression in your numWordsOfLength
method in the WordList
class once you've passed the list there as the previous answers suggest.如果您更喜欢使用第二个 class,您可以在
WordList
numWordsOfLength
中的 numWordsOfLength 方法中使用此“计数”表达式,一旦您按照前面的答案建议通过列表。
BTW, the loop in your WordList class seem to be incorrect: you're only iterating the list up to len
, instead you want to iterate over the entire list.顺便说一句,您的 WordList class 中的循环似乎不正确:您只是将列表迭代到
len
,而不是您想要迭代整个列表。 I assume the len
variable is the searched lengths of a word:我假设
len
变量是一个单词的搜索长度:
public int numWordsOfLength(int len, List<String> list){
int count = 0;
for(int i=0;i<list.size();i++){
if(list.get(i).length()==len){
count++;
}
}
return count;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.