• Post Reply Bookmark Topic Watch Topic
  • New Topic

confused on making an object and adding to array  RSS feed

 
Kip Bodey
Ranch Hand
Posts: 43
1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
i need to be able to call this method and it should take the object and, add it to my list of items, but im having trouble getting it to work, and i have spend weeks on this one step. PLEASE any help would be awesome, i know the numbers and stuff arent "correct eventually it will add one to the array length when i call add item, but im just trying to go one step at a time
 
Tim Cooke
Marshal
Posts: 4051
239
Clojure IntelliJ IDE Java
  • Likes 1
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Kip Bodey wrote:but im having trouble getting it to work

You need to be a bit more specific than that.
  • Does it compile?
  • Does it run?
  • What behaviour do you expect?
  • What behaviour do you observe?
  • What is wrong about all that you observe in the previous points?

  • As a general comment, this...

    ... is almost certainly not doing what you're expecting.
     
    Kip Bodey
    Ranch Hand
    Posts: 43
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    yeah you are right, I actually made a step in the right direction last night i think. I am going to work on it some more until i get confused again. thank you for your reply though
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Please show us what you have already.
     
    Kip Bodey
    Ranch Hand
    Posts: 43
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    i got that part working actually, but how about this one, i am trying to make an array, called items it should be able to hold up to 100 items, here is what i got

    am am getting an out of bounds error at the items[numberOfItems] = new MediaItem();
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    You need to provide a value for numberOfItems; I presume you have done so and not shown us.
    If that is 100, then you get a 100‑element array. Those elements are number 0…99, not 100. When you call array[size], then you are 1 beyond the bounds of the array. You want to fill the array, so you would have to loop and assign each element. The for of for loop you showed later will work nicely for filling the array.

    At least in Java® you get an Exception. If you did that in C you would get to an unrelated bit of memory and who know what would happen!
     
    Kip Bodey
    Ranch Hand
    Posts: 43
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    ok so i start my numberofitems at 1, which initializes the array with size one, when i add i tell it to add to items[numberofitems - 1] so it hits the zero location it is saying null pointer exception
    i mean am i doing this right? or should my array size just be 100 maybe? and when i print the list i tell it to leave out everything that doesnt have a value?
     
    James Boswell
    Bartender
    Posts: 1051
    5
    Chrome Eclipse IDE Hibernate
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator

    Have a think for a moment. What has items[numberOfItems-1] been set to before this line of code?

    Have a look at this post which highlights the default values for array elements: http://www.coderanch.com/t/502882/java/java/default-Array-elements
     
    Piet Souris
    Master Rancher
    Posts: 2044
    75
    • Likes 1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    hi Kip,

    no, unfortunately, it doesn't work that way. If you create an array of length 1, as you do now,
    then that's it. You cannot change the size later, when you need to store more items.

    you should set numberOfItems to 100, and you then have an array of 100, numbered 0...99.
    So:

    Initially, all the 100 locations are filled with 'null'. If you go through your array, one item at the time, you
    can check if the item is null, meaning no item at that location, or if it is not null, in which case it
    contains some 'MediaItem' in that location.

    But arrays are not very handy for these things. Have you ever heard of a thing called 'ArrayList' ?

    Greetz,
    Piet
     
    Kip Bodey
    Ranch Hand
    Posts: 43
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    yes i have heard of an array list, but dont know how to use them, this is for a project in class, while i am allowed to do what i want she suggests doing it how she says, and a normal array is what she says to use

    so why wont this work, i also tried using 0 instead of numberofitems here "items[numberOfItems].setTitle(title);"
     
    Kip Bodey
    Ranch Hand
    Posts: 43
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    thank you everyone, now on to my next problem but at least i got this to work thank you, this below works for now though


     
    Kip Bodey
    Ranch Hand
    Posts: 43
    1
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am having trouble getting my counter to work. i have created an array items[100] i want to be able to call a method to add things to it and im using numberOfItems to be the counter and its not seeming to keep the value

    i have it printing just to see where my index number is and it is staying at zero and im assuming it keeps overwriting my previous entry. what am i doing wrong here?
     
    Campbell Ritchie
    Marshal
    Posts: 56599
    172
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    I am afraid, quite a lot.
    Your library class should only have private fields, so you shouldn't be able to access them directly. You should have getXXX methods. The library class should set up the array and return a reference to it (or maybe, to myArray.clone() so the array cannot be manipulated outwith the library).

    I think you need to delete that method, disconnect your computer from the mains and get a sheet of paper. That code looks like somebody who is guessing. You cannot work things out in code, but you can on paper. Write down in plain English what you want to do. When you have got that worked out you will have some chance of getting the array working and you can turn your computer on again.
    There is a section of the Java Tutorials about arrays, but it is quite short. It does not tell you how to use a loop, because it appears before loops in the Tutorials.

    There is a field in arrays called length which tells you the size of the array. If you print it out and says 100, that means you have a 100‑array with indices 0…99. There is however no straightforward way to get an array to tell you how many of its elements have actually been initialised. You can iterate the array and count how many elements are not null. You can get a Stream and filter it and count the not null elements but that is an advanced feature. If you use a List as somebody suggested earlier, that ahs a method telling you what its size is.
    You should also look at the names you are giving to variables. You have a Library variable called count. Variable names should tell you what they mean, so count sounds a very poor name for a Library variable.
     
    Don't get me started about those stupid light bulbs.
    • Post Reply Bookmark Topic Watch Topic
    • New Topic
    Boost this thread!