• Post Reply Bookmark Topic Watch Topic
  • New Topic

Printing a list from a different method  RSS feed

 
W Wilson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Right, so I got this method that creates and sorts 2 lists. What I want to do is merge these lists into a third list, have it sorted, and then print the contents of the list. The problem is, I'm tired and I don't remember how I can print it.



 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
The java compiler is complaining that it cannot find a symbol aList in method main.
It is doing it because there is no such symbol declared in method main.

You have declared aList in method CHANGEME, right?
So why main cannot see it?

Well, aList is a so-called local variable.
It is declared inside a method and it is visible only insite this method.

No other method can see it.
 
W Wilson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:The java compiler is complaining that it cannot find a symbol aList in method main.
It is doing it because there is no such symbol declared in method main.

You have declared aList in method CHANGEME, right?
So why main cannot see it?

Well, aList is a so-called local variable.
It is declared inside a method and it is visible only insite this method.

No other method can see it.


Surely there must be a change to access it in some way without creating the lists in the main?
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
There are some options.
You can always pass a list as a method parameter.

One important note: you are not calling CHANGEME from main so its code will never be executed.

Second important thing (a litle offtop ).
Instead of declaring useThis way if you ever changed your mind and wanted a different type of list you would need to do less changes in your code.
Second, if you use java 1.7 or above this declaration can be simplified to

But do not simplify it like this: This would just use raw ArrayList without type-checking.
 
W Wilson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Paweł Baczyński wrote:There are some options.
You can always pass a list as a method parameter.

One important note: you are not calling CHANGEME from main so its code will never be executed.

Second important thing (a litle offtop ).
Instead of declaring useThis way if you ever changed your mind and wanted a different type of list you would need to do less changes in your code.
Second, if you use java 1.7 or above this declaration can be simplified to

But do not simplify it like this: This would just use raw ArrayList without type-checking.


Appreciate the heads up on the type-checking. I wondered if it was possible but I couldn't find a decently explained answer.

I added changeme() to main, I had forgotten to do so earlier it seems.

So if I was going to pass a list as a method parameter, how would that go? I know about passing primitive data types but I never really have passed like an array or a list before. I know I could always do things simpler and get away by doing everything within the main method, but where's the fun in that?
 
Paweł Baczyński
Bartender
Posts: 2083
44
Firefox Browser IntelliJ IDE Java Linux Spring
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Passing a list, an array or any other object as a method paramether looks the same as passing a primitive.
An example:
Also important. I didn't notice this before. To be able to run your program, you need to mark your main method as static.
 
W Wilson
Greenhorn
Posts: 26
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
And tada. Everything I wanted to do is done and appears to work fine. Seems I just need to add documentation. But I do have a general question about style/programming procedure.



When I pass these lists into the other methods, I do so as list1-3 instead of using their names of aList, bList, etc etc. Is this good practice or can it just make my program more confusing? I tried using both, and so it got the same result, but I'm wondering if using new names in the methods is bad practice that could cause confusion to someone else reading my code?
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!