0

I have a method:

public static ArrayList<Integer>Iterate(int n){
    ArrayList<Integer> numbers = new ArrayList<Integer>(n);{
        Random rand = new Random();
        rand.setSeed(System.currentTimeMillis());
        for(int i = 0; i<n; ++i){
            Integer r = rand.nextInt() %113;
            numbers.add(r);
            Collections.sort(numbers);
        }}

And I want to use other methods on this Array to find out certain things about the numbers.

But I can't figure out to call the Arraylist in public static void main(String [] args).

I've tried things like:

System.out.println( numbers.methodname); 

And things like that, but Eclipse says numbers cannot be resolved to a variable So what's the proper way to call the ArrayList so a method can effect it?

5
  • Declare numbers outside of the method Iterate. As it is now, only the method Iterate has access to that variable. Commented Oct 25, 2014 at 3:39
  • 3
    Please please please please read a basic guide to OOP or Java... Commented Oct 25, 2014 at 3:54
  • Your method is declared as returning an ArrayList<Integer>, so presumably you do return it, right? And you call it from main, right? What do you assign its return value to, in main? Commented Oct 25, 2014 at 4:24
  • Also, why do you sort the list every time you add a number to it? Commented Oct 25, 2014 at 4:24
  • It doesn't seem that you ever return an ArrayList<Integer> in your method. Just return numbers in your method and in main do something like ArrayList<Integer> list = Iterate(someValue). Then you can call methods on list. Commented Oct 25, 2014 at 4:55

3 Answers 3

1

You probably want to make your method return the ArrayList that it creates, so that it can be used by whatever method called your method. You've already got the right method signature - that is, the first line, where it says

public static ArrayList<Integer> Iterate(int n){

which means that the method will return something of type ArrayList<Integer>. Now you need to add this line to the bottom of the method, before the last }.

return numbers;

Now, within your main method, or whatever other method calls this one, you can write something like

ArrayList<Integer> returnedList = Iterate(10);

or something similar, depending on what number you want to pass to the method. This declares a variable called returnedList, calls the method, and assigns the ArrayList that's returned by the method, to the variable that you declared.

Sign up to request clarification or add additional context in comments.

Comments

0

Since you declared the numbers variable in the Iterate method, it does not exist outside the scope of that method. This means that while you're in main, you cannot access that variable. If you want to access a variable in main, it needs to be declared in main.

Comments

0

numbers needs to be declared outside of your method for other methods to be able to access it. If you declare a variable inside a method, its scope is the method it was declared in.

You can still initialize the variable inside the method if you so choose, but the declaration has to be made outside, like this:

ArrayList<Integer> numbers = null;

public static ArrayList<Integer> Iterate(int n){
    numbers = new ArrayList<Integer>(n);
    // code
}

2 Comments

Thanks, but when I tried this it said: "Cannot make a static reference to the non-static field numbers"
short answer to make it work: static ArrayList<Integer> numbers = null; -------------------------------- good answer: After you're done with this assignment or whatever it is, I suggest you read up or watch tutorials on basic OOP. Take it from someone who's rushed through assignments before too, you won't make it very far if you don't truly understand what you're coding.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.