0

I have a class in which I'm trying to create a 2D array with a starting value of 0 in every space (class1). I want that array to be able to be called in another class (class2), edit the values and then be stored back in the original class (class1) that it was created in to be called in another class (class3) with the values that were changed from the second class.

I know there must be some way to do this but if it's too complicated or not an efficient way I can find another way do what I'm trying to accomplish. If you can give me an example that would be great.

Right now I have a small class with.

public class Inventory {

    public static int[][] inventory_1 = {
        {0, 0, 0, 0, 0, 0}, 
        {0, 0, 0, 0, 0},
        {0, 0, 0, 0, 0},
    };
}

I'm using this temporarily as a placeholder for the array, but as expected it resets the contents to the original array when called from another class, or if a new instance of the second class is run.

Inventory inventorynew = new Inventory();
inventorynew.inventory_1 [0][0] = 1

What I'm actually doing is creating and inventory system and depending on what the value is in each place, it will determine what is in the inventory.

halfer
  • 19,824
  • 17
  • 99
  • 186
ADK
  • 5
  • 5
  • use `Inventory.inventory_1[0][0]` without creation of new class – Iłya Bursov Jun 30 '15 at 13:33
  • Top tips for asking good questions: don't add "help please" to titles, that's implicit - use descriptive titles that help readers understand the problem succinctly. Please also don't add requests for "constructive answers only" or some such - you were lucky in this case not to be bombarded with downvotes `:-)`. Keep descriptive text relevant to the problem at hand. Thanks! – halfer Aug 02 '15 at 08:14

3 Answers3

6
Inventory inventorynew = new Inventory();
inventorynew.inventory_1 [0][0] == 1

This is what you're doing wrong.

  • = : set a value

  • == : check a value

I suppose you are trying to set the value to 1, but you are checking if it is 1.

riccardo.cardin
  • 7,971
  • 5
  • 57
  • 106
Laurens
  • 121
  • 1
  • 12
  • Sorry, I should've used a different part of my code, but I have that correct. I just used a bad example. Thank you for the response though. – ADK Jun 30 '15 at 13:54
  • This doesn't answer the problem I'm having. Right now, i have an inventory class that is created to just give me the inventory format and/or store the values. I have 3 other classes that need to get the inventory information. When i call an inventory inventoyrnew = new inventory(); in the first class it is used in in, should i use something else to reference that information when i get in another class. I just want to the values to be persistent through the classes. – ADK Jul 15 '15 at 19:49
1

From the code:

Inventory inventorynew = new Inventory();
inventorynew.inventory_1 [0][0] == 1

Would imply that you are checking that the item in inventorynew.inventory_1 [0][0] is equal to the value "1" as opposed to setting the value of that item to "1". Note the differences between:

  • a = b - Set a to equal b
  • a == b - Is a equal to b?
ryanyuyu
  • 6,366
  • 10
  • 48
  • 53
Sean Bulley
  • 73
  • 1
  • 8
0

I see 2 solutions :

  1. Pass it as parameter, and get it back as return value
  2. Use a List instead, all your changes will be kept unless you create a new List.

Edit : maybe you should take a look here : Is Java "pass-by-reference" or "pass-by-value"?

Community
  • 1
  • 1
VLEFF
  • 533
  • 3
  • 14
  • Really? you're probably right, i use rarely arrays. But according to @Laurens answer, i thought he was trying to pass the array, but it seems that he's trying to pass the Inventory object... – VLEFF Jun 30 '15 at 13:39
  • If you pass an array reference and change its content by accessing certain indices, then yes it is like calling 'add' or 'remove' on a 'List' inside a method. – Tom Jun 30 '15 at 13:41
  • I actually instantly saw that the '=' sign was used incorrectly. Your solution probably works too, as long as you correctly use '=' – Laurens Jun 30 '15 at 13:42
  • Thank you for pointing me to that post, VLef. I think that is going to help immensely in this project and I'm pretty sure that is exactly what I'm looking for. – ADK Jun 30 '15 at 14:00