• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

Empty Array

 
Jamie Stokely
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21111
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 2
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
My bad for not posting my own code. Resolved this issue myself. Regardless, thank you for the help.
 
Rob Spoor
Sheriff
Pie
Posts: 20545
56
Chrome Eclipse IDE Java Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Campbell Ritchie wrote:

Why not just write new int[0]? It does the same thing but is shorter to write and easier to read.
 
Campbell Ritchie
Sheriff
Pie
Posts: 49367
62
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I was trying to show all possibilities. Agree you would usually write new int[0].
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Nitish Bangera
Ranch Hand
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


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
Marshal
Pie
Posts: 21185
80
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
Winston Gutkowski
Bartender
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 537
Eclipse IDE Java Python
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 10422
63
Eclipse IDE Hibernate Ubuntu
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic