-1

i am learning to code and am programming with object arrays at the moment.

My problem now: I want to add objects after the last stored object in my array when i push a button. cdarray.length-1 sets the value in the numberfield which holds my arrayid to 99. Thats not what i want. it should begin at 0 because thats, where i want to begin to add objects.

Can anyone help me ? :P

Would be nice

CD[] cdarray = new CD[100];
nf_objektID.setInt(cdarray.length-1);
cdarray[cdarray.length-1] = new CD();
Rob
  • 3
  • 1
  • 1
    Do you have to use an array? Why not use an `ArrayList` or something like it? – Mureinik Mar 10 '17 at 19:30
  • So you need to record how many elements you have in your array in a counter or use `ArrayList` because that's what it does for you (and resizes the array as needed) – Peter Lawrey Mar 10 '17 at 19:41
  • This is the answer to your question, I guess: http://stackoverflow.com/questions/4441099/how-do-you-count-the-elements-of-an-array-in-java In general (like @Mureinik suggest) you seem to try to re-invent the wheel. Arrays perform really well, but if performance isn't crucial, try to take a look at Collections (and maps). They're huge and most probably you're after https://docs.oracle.com/javase/8/docs/api/java/util/List.html. Keep learning! ;-) – Piohen Mar 10 '17 at 19:44
  • Hm. Thank you. Arraylists are fine too. Got to try this.Do I have to use a constructor to set the values of the object? for example if i want to add an artist and a title of the cd?! this works with cdarray.add("Michael Jackson", "Thriller"); ? – Rob Mar 10 '17 at 19:46
  • You have two separate things going on here. The physical size of the underlying array, and the last location written to (which would be something like the virtual size). So the physical size=100 in your example. Then if you create another variable called 'loc', which is initialized to 0. You would then do cdArray[loc++] = myNewValue (and note that 99 is the largest that loc is allowed to get to) – Oscar Mar 10 '17 at 20:09
  • Thank you. This works. But how can i do this with an arraylist? Get the current (the item thats going to be filled next) Listitems ID and print it in a numberfield?! ArrayListcdarray = new ArrayList(); and then nf_objektID.setInt(cdarray.size()); – Rob Mar 11 '17 at 12:39

1 Answers1

1

I love it when beginners want to learn the implementation instead of using existing library. Some will say you're reinventing wheel, but ignore that. You learn the basics and you'll master the science. To answer you question, look at the source code of ArrayList.add()

 329:   /**
 330:    * Appends the supplied element to the end of this list.
 331:    * The element, e, can be an object of any type or null.
 332:    *
 333:    * @param e the element to be appended to this list
 334:    * @return true, the add will always succeed
 335:    */
 336:   public boolean add(E e)
 337:   {
 338:     modCount++;
 339:     if (size == data.length)
 340:       ensureCapacity(size + 1);
 341:     data[size++] = e;
 342:     return true;
 343:   }
Kay
  • 393
  • 4
  • 15
  • Thank you. How can you access Source code of Java implemented classes and methods by the way? – Rob Mar 11 '17 at 12:42