怎么将其他类的字符串返回到主类的arraylist中,并用for循环分别存两个index里面,代码如下,在线等,非常感谢!
public class test{
public String index11(){
return "aa";
}
public String index12(){
return "bb";
}
public String index21(){
return "cc";
}
public String index22(){
return "dd";
}
}
如何将其他类的字符串返回到主类的arraylist中,谢谢!
答案:1 悬赏:50 手机版
解决时间 2021-12-25 21:04
- 提问者网友:皆是孤独
- 2021-12-25 10:49
最佳答案
- 五星知识达人网友:第四晚心情
- 2021-12-25 11:11
import java.lang.reflect.Method;
import java.util.ArrayList;
public class TestS {
public static void main(String[] args) throws Exception{
test t = new test();
Class<? extends test> testc = t.getClass();
Method[] m = testc.getDeclaredMethods();
ArrayList<String> al = new ArrayList<String>();
for (int i = 0; i < m.length; i++) {
if (m[i].getReturnType() == String.class){
al.add(m[i].invoke(t).toString());
}
}
System.out.println(al);
}
}
class test{
public String index11(){
return "aa";
}
public String index12(){
return "bb";
}
public String index21(){
return "cc";
}
public String index22(){
return "dd";
}
public int index111(){
return 1;
}
}
import java.util.ArrayList;
public class TestS {
public static void main(String[] args) throws Exception{
test t = new test();
Class<? extends test> testc = t.getClass();
Method[] m = testc.getDeclaredMethods();
ArrayList<String> al = new ArrayList<String>();
for (int i = 0; i < m.length; i++) {
if (m[i].getReturnType() == String.class){
al.add(m[i].invoke(t).toString());
}
}
System.out.println(al);
}
}
class test{
public String index11(){
return "aa";
}
public String index12(){
return "bb";
}
public String index21(){
return "cc";
}
public String index22(){
return "dd";
}
public int index111(){
return 1;
}
}
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯