• 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

Is there a method to find what part of array has been filled

 
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Supposing I have an array with its length as 10.I have filled six of it.The method should return 6. Now i would like to if there is any method in API to know what part of the Array has been filled.
 
Sheriff
Posts: 14691
16
Eclipse IDE VI Editor Ubuntu
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator

Now i would like to if there is any method in API to know what part of the Array has been filled.


What do you exactly want ? An array of indexes ? An array of elements ? I don't think that there is such methods in the API.
 
ramanuja varun
Ranch Hand
Posts: 47
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Its an array of elements
 
Ranch Hand
Posts: 87
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
Do you have to use an array? In the Collections framework in java.util, there are a lot of classes dealing with effective element storage and retrieval.
 
Marshal
Posts: 79177
377
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
I don't think there is such a method, I am afraid.
An array has a fixed length.

There is a potential workaround; the elements of an array are a bit like instance fields in that they have default values; booleans default to false, numbers default to 0 (or 0L 0d or 0f) and reference types default to null. You can iterate through the array counting how many elements are different from their default types. Of course if you "fill" an array with 0s that technique will give a totally misleading answer.

Alternative: create a class which incorporates an array and methods to insert and retrieve values. You can have counters for how many values have been inserted, or an array of booleans which change from false to true or vice versa whenever a value is inserted first time, and use those to count insertions.
 
There will be plenty of time to discuss your objections when and if you return. The cargo is 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