• Post Reply Bookmark Topic Watch Topic
  • New Topic

How do i add the actual content in Java to the ArrayList instead of the memory location  RSS feed

 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hi,

I am creating Java code for a job interview and when adding an item to an ArrayList i keep adding only the memory location into the List not the actual item with id .. etc.

My code contains 3 files and the issue is at "itemList.add(item);" in the 1st class "Packer".

Class 1

   

Class 2

   

Class 3

   
 
Arend van der Kolk
Ranch Hand
Posts: 78
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I forgot to add what the program actually does. It reads from the file attached and chooses the best item with max value.

And this is an other example that does work correctly on the file: https://github.com/gavinc88/MiniProjects/blob/master/Package.java

Make a text (.txt) file with following content to the test the program(of course change the Directory in class 1 to your own directory):

81 : (1,53.38,€45) (2,88.62,€98) (3,78.48,€3) (4,72.30,€76) (5,30.18,€9) (6,46.34,€48)
8 : (1,15.3,€34)
75 : (1,85.31,€29) (2,14.55,€74) (3,3.98,€16) (4,26.24,€55) (5,63.69,€52) (6,76.25,€75) (7,60.02,€74) (8,93.18,€35) (9,89.95,€78)
56 : (1,90.72,€13) (2,33.80,€40) (3,43.15,€10) (4,37.97,€16) (5,46.81,€36) (6,48.77,€79) (7,81.80,€45) (8,19.36,€79) (9,6.76,€64)
 
Stefan Evans
Bartender
Posts: 1835
10
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I don't see anything overtly wrong here.

"when adding an item to an ArrayList i keep adding only the memory location into the List not the actual item with id"

Why do you think this?
What proof do you have?
hint:  check out the default implementation of toString() for an object...
 
Campbell Ritchie
Sheriff
Posts: 54495
150
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Another thing: if you are using that code for an interview, you will have problems. For example the pack() method is too long. For another, you probably shouldn't use com.packer as a package name. That package name belongs to somebody else, as you will find out if you go through the Java™ Tutorials.
If you are using a Scanner to read lines from the file, why are you then using String#split and String#trim? Wouldn't it be better to create a regex which strips the whitespace, too? Or use a second Scanner?
Why does the Packer class have all static methods?
What does line 28 mean? Why have you got a capital W?
What does all the commented‑out code mean?
Why is the text file in that particular location on your disc?

Stephan is, I think ahead of me. I fail to understand what you mean about adding the contents rather than memory locations in the List. What did you expect the List to contain? The whole idea of object‑orientation is that you retain the information in the object, so the contents of the String live in the String object. Why would you expect the List to contain the contents of the String object rather than a reference to the object?
 
Liutauras Vilda
Marshal
Posts: 4117
227
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Line 21.

Don't use File object. Use Path (since Java 1.7) object instead, its use:
File has its own flaws used along with Scanner. We have seen it in practice.
 
Liutauras Vilda
Marshal
Posts: 4117
227
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Arend van der Kolk wrote:

This constructor. Probably not the constructor itself but its parameters. In particular second and third. I think they are very very confusing because of presence of them two in the same place.

Third parameter I'd expect to be "items". Second, probably "itemsCount", in fact - you don't need probably that second if indeed it represents items count, because you could get that by issueing "itemList.size()".

By the way, parameter "weight", why is it an int? Is it in grams?
 
Liutauras Vilda
Marshal
Posts: 4117
227
BSD
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Also, you got weird indentation, I've never seen it before. Package statement supposed to be aligned with import statements.

Another thing, you got quite a few static methods in class 1 (Packer). It might makes sense to have them static or might not, but why they are in the class Packer? What method removeParenthesis has to do with Packer class?

Then, you have method "pack" where you're passing in file path, so what are you packing? Isn't it confusing? Non descriptive?

I'd expect you to open file, which is in the path... but not pack it. Method names supposed to make sense, so everybody reading could understand what your program does. As a proof you can see that Campbell failed to understand, it suggests that program is confusing.

I'd suggest you to keep structure like:

Now, if you follow that advice, you'll get procedural code, but from that you'd have more chances to make it object oriented. Give a try if you willing to and see if you like it more than what you have now.
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!