1

## JAVA 函數整合問題

``````public List<String> getRandom1Ary(ArrayList<ExamItemsBean> items, int count)
{
List<String> resultAry = new ArrayList<String>();
Random random = new Random();
do
{
int next = random.nextInt(items.size());
ExamItemsBean item = items.get(next);
if (!resultAry.contains(item.getAA()))
{
}
} while (resultAry.size() < count);
return resultAry;
}

public List<String> getRandom2Ary(ArrayList<ExamItemsBean> items, int count)
{
List<String> resultAry = new ArrayList<String>();
Random random = new Random();
do
{
int next = random.nextInt(items.size());
ExamItemsBean item = items.get(next);
if (!resultAry.contains(item.getBB()))
{
}
} while (resultAry.size() < count);
return resultAry;
}
``````

### 3 個回答

0
dragonH
iT邦大師 7 級 ‧ 2019-08-23 11:15:03

``````item.getXX()
``````

``````public List<String> getRandomAry(ArrayList<ExamItemsBean> items, int count, String target)
{
List<String> resultAry = new ArrayList<String>();
Random random = new Random();
do
{
int next = random.nextInt(items.size());
ExamItemsBean item = items.get(next);
if (!resultAry.contains(item.getItem(target)))
{
}
} while (resultAry.size() < count);
return resultAry;
}
``````

dragonH iT邦大師 7 級 ‧ 2019-08-23 11:19:41 檢舉

0
e6319a5b
iT邦新手 5 級 ‧ 2019-08-23 16:53:15
``````public class MethodAsParams {
public static void main(String s[]) throws Exception {
// we assume that called methods have no argument
Object paramsObj[];

String[]hiStrings={"hi","，","hello"," ","World!!!"};

paramsObj =new Object[]{hiStrings};

Method thisMethod = Execute.class.getDeclaredMethod("go", paramsObj.getClass());//只有getClass(得到類型) 不是實例

new UseMethodAsParaments(thisMethod, new Execute(), paramsObj);

}

}
class UseMethodAsParaments{
Method method;
Execute e;
public UseMethodAsParaments(Method method,Object e,Object[]objs) throws IllegalAccessException, IllegalArgumentException, InvocationTargetException{
this.method=method;
method.invoke(e, objs);
}
}
class Execute{
public void go(Object[] args){
System.out.print("work	");
for (int i = 0; i < args.length; i++) {
System.out.print(args[i]);

}
}
}
``````

e6319a5b iT邦新手 5 級 ‧ 2019-08-24 13:06:02 檢舉

1.不需要
2.

``````public class MethodAsParams {
public static void main(String s[]) throws Exception {
// we assume that called methods have no argument

List<ExamItemsBean>examItemsBeanList=new ArrayList<ExamItemsBean>();
for (int i = 0; i < 50; i++) {
ExamItemsBean tempBean=new ExamItemsBean();
tempBean.Acontent=String.valueOf(i);
tempBean.Bcontent=String.valueOf(i+100);

}

List<String>temp4Show=examItemsBeanList.get(0).getRandom1Ary(examItemsBeanList, 30, "getLargeThan100");
for (String string : temp4Show) {
System.out.print(string+"\n");
}

}

}

class ExamItemsBean{

public String Acontent="",Bcontent="";

String getSmallThan50() {
return Acontent;
}
String getLargeThan100() {
return Bcontent;
}

public List<String> getRandom1Ary(List<ExamItemsBean> items, int count,String methodName) throws NoSuchMethodException, SecurityException
{
Method thisMethod = ExamItemsBean.class.getDeclaredMethod(methodName, null);
List<String> resultAry = new ArrayList<String>();
Random random = new Random();
do
{
int next = random.nextInt(items.size());
ExamItemsBean item = items.get(next);
try {
if (!resultAry.contains((String)thisMethod.invoke(item, null)))
{
}
} catch (IllegalAccessException e) {
// TODO 自動產生的 catch 區塊
e.printStackTrace();
} catch (IllegalArgumentException e) {
// TODO 自動產生的 catch 區塊
e.printStackTrace();
} catch (InvocationTargetException e) {
// TODO 自動產生的 catch 區塊
e.printStackTrace();
}
} while (resultAry.size() < count);
return resultAry;
}

}
``````

e6319a5b iT邦新手 5 級 ‧ 2019-08-25 16:55:48 檢舉

0

iT邦新手 5 級 ‧ 2019-08-27 12:26:15

https://docs.oracle.com/javase/8/docs/api/java/util/function/package-summary.html

``````//使用方式
getRandomAry(items, 0, bean -> bean.getAA());
getRandomAry(items, 0, bean -> bean.getBB());
getRandomAry(items, 0, bean -> bean.getCC());
``````
``````//增加 function 參數
public static List<String> getRandomAry(ArrayList<ExamItemsBean> items, int count, Function<ExamItemsBean,String> function) {
List<String> resultAry = new ArrayList<String>();
Random random = new Random();
do {
int next = random.nextInt(items.size());
ExamItemsBean item = items.get(next);
if (!resultAry.contains(function.apply(item))) {
}
} while (resultAry.size() < count);
return resultAry;
}
``````

https://developer.android.com/studio/write/java8-support