-1

I have the code below, two classes, one is the main class and the other is class1 which has the function foo1() this function make too many iterations over an ArrayList<> . the foo1() function is called once in the main class, and then the size function is called.

The problem with my code is that the function getSize() is making iteration again to get the size of the function.

What i need is to get the size of the already called function without losing the information about it. and not to call the function again and getting the size because its time consuming. I thought about making an attribute in the class1 and then assign the size of the array into the attribute like the following: but its not a good choice i think, so i am looking for a professional approach.

import java.util.*;

public class HelloWorld {
  public static void main(String []args){
    class1 c = new class1();
    // foo1 function is called
    System.out.println(c.foo1());
    // get size is called, this should be in another form
    System.out.println(c.getSize());
  }     
}

public class class1{
  int size = 0;

  public ArrayList<Integer> foo1() {
    ArrayList<Integer> result = new ArrayList<>();
    for(int i = 0;i<1000;i++){
      result.add(i);
    }

    return result;
  }    

  public int getSize(){
    return foo1().size();
  }
}  

My solution which is not popular.

public class class1 {
  int size = 0;

  public ArrayList<Integer> foo1(){
    ArrayList<Integer> result = new ArrayList<>();
    for(int i = 0;i<1000;i++){
      result.add(i);
    }

    // assigning
    size = result.size();
    return result;
  }

  public int getSize() {
    return size;
  }
}
Corey Ogburn
  • 24,072
  • 31
  • 113
  • 188
Alex
  • 267
  • 1
  • 3
  • 18
  • I would suggest doing `int size = -1`, and in your getSize method, do `if (size == -1) size = foo1().size(); return size;` However, this doesn't look like good design, and I'd suggest putting a question on Code Review – user May 15 '20 at 18:31
  • 1
    Please format your code better in the future. As a site built around code, we appreciate proper indentation. – Corey Ogburn May 15 '20 at 18:36
  • 1
    In `foo1()`, you are creating a new `ArrayList` and adding items to it by iterating `1000` times. Why do you think that the size of this `ArrayList` will be other than `1000`? If you already know the no. of iterations, that is also the size of this `ArrayList`. Therefore, the method, `getSize()` doesn't make sense at all. – Arvind Kumar Avinash May 15 '20 at 19:07
  • @ Arvind Kumar Avinash this is just an example represents my actual problem in a big software – Alex May 15 '20 at 19:10
  • 1
    Instead of describing your problem in terms of how to get size, you should describe what you are actually trying to do. I don't see any benefit from your current design. – WJS May 15 '20 at 21:24

1 Answers1

1

Store returned value (Arraylist) in a variable like this : Arraylist result = c.foo1() and then call size() on this variable like: result.size()

Kunal Patil
  • 745
  • 1
  • 9
  • 18
  • with declaration of variable `result = c.foo1()` the c.foo1() will be called again. what i want is to gain the informtaion of the first call and dont need to call the function again. – Alex May 15 '20 at 18:43
  • Don't call `c.foo1()` twice. Store the result Arraylist only once. Edited my answer. – Kunal Patil May 15 '20 at 18:49