Reputation: 107
I can't figure out how to access these indexes. I'm lost with the appropriate flow.
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;
}
I need to access this index, and get the values of the list in main method..I'm calling the method as
getIndex(positiveValues, nvalues);
But that's all I know..
Upvotes: 0
Views: 574
Reputation: 5233
First, to get you going, positiveList.indexOf(positiveList.get(i));
returns i
, think about what you do here.
What you obviously want is a list of arrays that fulfill a certain condition for a given list. Therefore, returning only a single int index
is not the solution. Try to return a list of all indices, like in this example:
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;
}
With this list, you can do whatever you want in your main method now.
Upvotes: 0
Reputation: 2641
private static int getIndex(List<Double> positiveList, double nvalues)
make it public method so you can access as util method.
int index =YourClassNmae.getIndex(positiveValues, nvalues);
or inside same class can call directly
int index =getIndex(positiveValues, nvalues);
Upvotes: 0
Reputation: 784
int index = getIndex(positiveValues, nvalues);
You create a variable index and stocks the returned value of getIndex in variable index.
Upvotes: 1