- import java.lang.reflect.Method;
- import java.util.ArrayList;
- import java.util.List;
- public class Demo1 {
- public static void main(String[] args) throws Exception {
- List<Integer> list = new ArrayList<Integer>(); //定义Integer泛型
-
- String str = "abc";
-
- Method[] method=list.getClass().getMethods();//取得list的所有方法
-
- System.out.println(method.length);
-
- for(int i=0;i<method.length;i++){
-
- System.out.println(method[i]+"----");//遍历打印list的方法
-
- }
- method[0].invoke(list, str);//通过反射来执行list的第一个方法,第一个是list对象,代表该对象的方法,第二个是方法参数
-
- System.out.println(list.size());
-
- for(int i=0;i<list.size();i++){
-
- System.out.println(list.get(i));
-
- }
- }
- }
复制代码
在我这边怎么没有错啊???
以下是运行结果
35
public boolean java.util.ArrayList.add(java.lang.Object)----
public void java.util.ArrayList.add(int,java.lang.Object)----
public java.lang.Object java.util.ArrayList.get(int)----
public java.lang.Object java.util.ArrayList.clone()----
public int java.util.ArrayList.indexOf(java.lang.Object)----
public void java.util.ArrayList.clear()----
public boolean java.util.ArrayList.contains(java.lang.Object)----
public boolean java.util.ArrayList.isEmpty()----
public int java.util.ArrayList.lastIndexOf(java.lang.Object)----
public boolean java.util.ArrayList.addAll(int,java.util.Collection)----
public boolean java.util.ArrayList.addAll(java.util.Collection)----
public int java.util.ArrayList.size()----
public java.lang.Object[] java.util.ArrayList.toArray(java.lang.Object[])----
public java.lang.Object[] java.util.ArrayList.toArray()----
public boolean java.util.ArrayList.remove(java.lang.Object)----
public java.lang.Object java.util.ArrayList.remove(int)----
public java.lang.Object java.util.ArrayList.set(int,java.lang.Object)----
public void java.util.ArrayList.ensureCapacity(int)----
public void java.util.ArrayList.trimToSize()----
public int java.util.AbstractList.hashCode()----
public boolean java.util.AbstractList.equals(java.lang.Object)----
public java.util.Iterator java.util.AbstractList.iterator()----
public java.util.List java.util.AbstractList.subList(int,int)----
public java.util.ListIterator java.util.AbstractList.listIterator(int)----
public java.util.ListIterator java.util.AbstractList.listIterator()----
public java.lang.String java.util.AbstractCollection.toString()----
public boolean java.util.AbstractCollection.containsAll(java.util.Collection)----
public boolean java.util.AbstractCollection.removeAll(java.util.Collection)----
public boolean java.util.AbstractCollection.retainAll(java.util.Collection)----
public final void java.lang.Object.wait() throws java.lang.InterruptedException----
public final void java.lang.Object.wait(long,int) throws java.lang.InterruptedException----
public final native void java.lang.Object.wait(long) throws java.lang.InterruptedException----
public final native java.lang.Class java.lang.Object.getClass()----
public final native void java.lang.Object.notify()----
public final native void java.lang.Object.notifyAll()----
1
abc
|