• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

Empty Array

 
Greenhorn
Posts: 2
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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!
 
Ranch Hand
Posts: 537
Eclipse IDE Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
My bad for not posting my own code. Resolved this issue myself. Regardless, thank you for the help.
 
Sheriff
Posts: 22784
131
Eclipse IDE Spring VI Editor Chrome Java Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Marshal
Posts: 79239
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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?
 
Bartender
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 23951
142
jQuery Eclipse IDE Firefox Browser VI Editor C++ Chrome Java Linux Windows
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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 Python Java
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 10780
71
Hibernate Eclipse IDE Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
 
Oh. Hi guys! Look at this tiny ad:
a bit of art, as a gift, that will fit in a stocking
https://gardener-gift.com
reply
    Bookmark Topic Watch Topic
  • New Topic