Suppose you have a supermarket, and every item which belongs in that supermarket is stored in one place.

Then you have sections of the supermarket like "Food & Cuisine", "Entertainment" and so on.

The user of your program needs to be able to list every item within the supermarket, then distinctly list every item within each of the sections. When printing data, the same item must not be repeated.

So I believe what would need to be done is that everytime a new item is added - it will need to be stored in an initial place (in an ArrayList or something), then it will also need to be stored in each section (for which it belongs). But I'm worried this may be 'duplication' of data since the same information is being stored in two different places.

This method would mean that everytime an item needs to be deleted - it will need to be deleted from the initial list of all items in the market, it would also need to be deleted from whichever sections they belong which would mean the program's running costs will increase a lot in very little time. Is there a way that instead of storing the data in two places, the data can just be referenced from the initial list of all items?

yes

Please expand...

You need to keep clear in your mind the distinction between a Java object and a Java reference. The only way to create an object is via the "new" keyword*.
The things that go in variables and ArrayLists are references top objects. If I say

Item i = new Item("Cabbage");
Item i2 = i;
myArrayListOfItems.add(i);

I still have only 1 object and 3 references to it.
You have no control over where an object is stored. The Java VM allocates storage for new objects somewhere in its memory, and keeps track of it. All you can do is create references to it.
When you say "it will need to be stored in an initial place (in an ArrayList or something), then it will also need to be stored in each section" then you are mistaken. In Java, a reference to it is stored in the initial place, and a second reference to it is stored in each section. There is still only one copy of the Item, somewhere in the JVM's memory.
It's also a mistake to think that the references duplicate anything at all. The existance of a reference in "initial place" asserts that this item is known to the store. The existence of another reference in "Entertainment" asserts that this is an entertainment item - which is new information.
Deleting references is no big deal. Deleting references to an item in two places isn't going to take any significant time unless you are talking about deleting millions of items is many places. If you really really believe that deleting those references is going to be a problem then, in the Item class, keep a reference to each of the places where its is referenced. Then you can delete those references in almost constant time regardless of number of items.


* actually you can create an object as a literal at compile time, but that's not relevant here

Ah okay so to delete from the "initial list" and from the "references" I will need to do...
initialArrayList.remove(i);
foodArrayList.remove(i);

to remove from both places. This is what I have been doing at the moment but I was hoping if there was a way to simply just do initialArrayList.remove(i) and it would delete that object from the initialArrayList and every other section such as foodArrayList etc. all by itself.

The only problem with that is if there are many many such lists you will have to go to all of them to remove any possible references. If that really is a problem then use the solution I mentioned - in Item have

ArrayList<List> placesWhereThisIsReferenced = new ArrayList<List>();

then whenever you add this item to a list, add the list to placesWhereThisIsReferenced
The to delete the Item everywhere

for (list list : placesWhereThisIsReferenced) {
   list.remove(this);
}

Ahh right, thanks for the detailed help!

Much appreciated.

yes

ow boooh... asking a yes/no question without giving any proof of having done anything yourself, and marking it as a bad answer when I answer 'yes'.

you want an explanation? try for yourself first. I 'd rather help the so called (pro-verbial) moron who's at least trying, than doing all the work for a student who's to lazy to try and wants to "cruise by" on the answers someone else provides.

still feel the need to mark my answer as rubbish, than at least have the decency to say who you are, and, most important: WHY

commented: Just tries to patronize people without giving them actual help. -1
commented: I disagree with asif's assessment. +16

Thanks for reminding me, just gave you another -1 on reputation points with the reason why.

Hope you feel better now.

what you posted was a very vague description that could be used for ... tons of questions. I didn't try to patronize, as much as getting you to TRY to implement something in the line of what you needed before simply asking.

I didn't know that providing evidence (in code) for what you've attempted was a neccessity here.

Anyway, there may be many reasons I can't show the code, one of them being if it was to be used in a commercial setting - it doesn't reflect well if someone finds that the developer of their system was asking help online. Which is the reason I often tend not to display the code and others don't seem to mind as long as I've explained everything like I did here.

forum rules
under the last thread. I don't mind you asking questions, nor do I mind it if you are restricted as to what you can or can't show on the forum, but I do follow a bit the:

Do provide evidence of having done some work yourself if posting questions from school or work assignments

rule.

I see a lot of posts passing where the OP's intention is just to get their (home)work done for them, and I admit, I tend to react in a way they don't really like to get as an answer.

Since I knew it was possible, yet I didn't know exactly how. A lot of times I check the forum at work (yeah, I shouldn't, I know, but anyway ..) so, I don't have too much time I can spend on an answer.

My original answer was not ment to insult or 'attack' you in any way, I just knew it was possible, hence, my answer, 'yes'.

Okay I understand that but trust me the kind of person I am, I can never not try something to the best of my ability before askinf ro help, and I may have overreacted. Apologies from my side!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.