[英]accessing the return value of a method in java
我不知道如何訪問這些索引。 我迷失了適當的流程。
private static int getIndex(List<Double> positiveList, double nvalues) { int index = 0; for(int i = 0; i < positiveList.size(); i++){ if(positiveList.get(i)<=nvalues){ index = positiveList.indexOf(positiveList.get(i)); } } return index; }
我需要訪問該索引,並在main方法中獲取列表的值。
getIndex(positiveValues, nvalues);
但這就是我所知道的。
int index = getIndex(positiveValues, nvalues);
您創建一個變量索引,並將getIndex的返回值存儲在變量索引中。
首先,開始吧, positiveList.indexOf(positiveList.get(i));
返回i
,想想你在這里做什么。
您顯然想要的是滿足給定列表條件的數組列表。 因此,僅返回單個int index
不是解決方案。 嘗試返回所有索引的列表,例如以下示例:
private static ArrayList<Integer> getIndex(List<Double> positiveList, double nvalues) {
ArrayList<Integer> indices = new ArrayList<Integer>(); //something like this
for(int i = 0; i < positiveList.size(); i++){
if(positiveList.get(i)<=nvalues){
indices.append(i); //add an index to the list
}
}
return indices;
}
使用此列表,您現在就可以在主方法中執行任何操作。
private static int getIndex(List<Double> positiveList, double nvalues)
使其成為公共方法,因此您可以作為util方法進行訪問。
int index =YourClassNmae.getIndex(positiveValues, nvalues);
或在同一個班級內可以直接致電
int index =getIndex(positiveValues, nvalues);
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.