wood burning stoves 2.0*
The moose likes Beginning Java and the fly likes Empty Array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "Empty Array" Watch "Empty Array" New topic
Author

Empty Array

Jamie Stokely
Greenhorn

Joined: Jul 28, 2012
Posts: 2
My instructor wants me to write a program that will process an array of integers that will be of arbitrary size, with arbitrary values. He will then fill the array when hes grading my program. The program then does some other procedures which i won't get into now. I've been trying to add an empty array that takes input from the user that will give the array its size, and the values of its elements. However, whenever i try to declare an empty array i get an "array dimension missing" error. Little help?
Paul Clapham
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

You didn't post any code, which is unfortunate because you were asking about code that you wrote.

So I'm going to guess that you wrote "int array[] = new int[]" which doesn't say how many entries you want to put in the array. If that's the case, then say how many entries you want in the array. You do have other places where you've declared arrays to compare to, don't you? And if my guess was wrong, let's have a look at the code you're asking about.

And, welcome to the Ranch!
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Take a look at the Collection framework provided by Java. If you want some more out of it, you can look into Google's collection framework Guava.


[ SCJP 6.0 - 90% ] , JSP, Servlets and Learning EJB.
Try out the programs using a TextEditor. Textpad - Java 6 api
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
You might find array initialisers usefulAs long as you declare the boolean called noResult appropriately, I think all that code will compile.
Jamie Stokely
Greenhorn

Joined: Jul 28, 2012
Posts: 2
My bad for not posting my own code. Resolved this issue myself. Regardless, thank you for the help.
Rob Spoor
Sheriff

Joined: Oct 27, 2005
Posts: 19653
    
  18

Campbell Ritchie wrote:

Why not just write new int[0]? It does the same thing but is shorter to write and easier to read.


SCJP 1.4 - SCJP 6 - SCWCD 5 - OCEEJBD 6
How To Ask Questions How To Answer Questions
Campbell Ritchie
Sheriff

Joined: Oct 13, 2005
Posts: 38007
    
  22
I was trying to show all possibilities. Agree you would usually write new int[0].
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Campbell Ritchie wrote:You might find array initialisers usefulAs long as you declare the boolean called noResult appropriately, I think all that code will compile.


Don't you think using Collections would be better compared to making different array objects and then some of these object won't have a reference and would be garbage collected?
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Nitish Bangera wrote:Don't you think using Collections would be better compared to making different array objects and then some of these object won't have a reference and would be garbage collected?

I agree that using collections is usually better, but in this case it appears that Jamie was told to use arrays. I'm also not quite sure where the garbage-collection comes in; an array will be garbage-collected just the same as a List - when it goes out of scope.

Winston


Isn't it funny how there's always time and money enough to do it WRONG?
Articles by Winston can be found here
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537



What would happen to the original new int[0] when new int[] {1,2,3,4} is assigned to numbers? Out of scope is a different thing. Also, it would be a negligible thing in short programs.
Henry Wong
author
Sheriff

Joined: Sep 28, 2004
Posts: 18529
    
  40

Nitish Bangera wrote:

What would happen to the original new int[0] when new int[] {1,2,3,4} is assigned to numbers? Out of scope is a different thing. Also, it would be a negligible thing in short programs.


Array references behaves just like any other object references -- arrays are objects after all. The array reference will simply refer to the newly assigned array. And if the old array is no longer reachable, it will be eligible for garbage collection.

Henry


Books: Java Threads, 3rd Edition, Jini in a Nutshell, and Java Gems (contributor)
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Nitish Bangera wrote:What would happen to the original new int[0] when new int[] {1,2,3,4} is assigned to numbers?

An array is an object, just like any other, so it behaves exactly as it would if it was:
List<Integer> numbers = new List<Integer>();
...
numbers = Arrays.asList(1, 2, 3, 4);

or some other code that reassigns the variable.

Out of scope is a different thing.

I realise that. I'm afraid it's you who wasn't specific.

Winston
Nitish Bangera
Ranch Hand

Joined: Jul 15, 2009
Posts: 537

Well that's exactly my point about the array object being garbage collected. I guess i wasn't specific and clear about it.
Winston Gutkowski
Bartender

Joined: Mar 17, 2011
Posts: 7545
    
  18

Nitish Bangera wrote:Well that's exactly my point about the array object being garbage collected. I guess i wasn't specific and clear about it.

So you do understand that there's absolutely no difference between the collectability of an array, and that of a List or any other collection?
What I think you might be thinking of is the collectibility of its elements: If you remove() an element from a List, it is generally available for collection, unless referenced anywhere else; but an array has no such method so it doesn't apply. You can, of course, null out an element, which will have the same effect.

Winston
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: Empty Array
 
Similar Threads
Array questions
array size 0
Shared resource synchronization
File upload using FTP
Playing around with 2D arrays