Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

使用instanceof分类 #211

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 17 additions & 5 deletions src/main/java/com/github/hcsp/inheritance/Classifier.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,17 +19,29 @@ public static void main(String[] args) {
}

/**
* 给定一个包含任意对象的列表,将其按照以下方式分类: 如果对象是Number类型,将其放入numberList; 如果对象是String类型,将其放入stringList;
* 给定一个包含任意对象的列表,将其按照以下方式分类:
* 如果对象是Number类型,将其放入numberList;
* 如果对象是String类型,将其放入stringList;
* 否则,将其放入otherList。
*
* @param list 给定的包含任意对象的列表
* @param list 给定的包含任意对象的列表
* @param numberList 用于接收所有Number对象的列表
* @param stringList 用于接收所有String对象的列表
* @param otherList 用于接收其余所有类型对象的列表
* @param otherList 用于接收其余所有类型对象的列表
*/
public static void classify(
List<Object> list,
List<Number> numberList,
List<String> stringList,
List<Object> otherList) {}
}
List<Object> otherList) {
for (Object object : list) {
if (object instanceof Number) {
numberList.add((Number) object);
} else if (object instanceof String) {
stringList.add((String) object);
} else {
otherList.add(object);
}
}
}
}