Akshay Kumar
Akshay Kumar

Reputation: 107

accessing the return value of a method in java

  1. I have a method where I'm trying to get the index of positive list, which satisfies specific condition on modulus of negative list. I'm getting the index of which satisfies, however I also need to access this index in the main method in order to access the positive values only which satisfied that condition.
  2. 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;
    
    }
    
  3. 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

Answers (3)

PKlumpp
PKlumpp

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

gati sahu
gati sahu

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

Jonas
Jonas

Reputation: 784

int index = getIndex(positiveValues, nvalues);

You create a variable index and stocks the returned value of getIndex in variable index.

Upvotes: 1

Related Questions