This is driving me mad. (Map problem...)

I've been programming a java text-based game for quite a while now. Unfortunately I'm having a problem.
Simple version---
For some reason I use thismap = thatmap. And when I modify thatmap it also changes thismap
Long Version---
First I have a map for the itemtemplate to be copied.
Next it copies the itemtemplate into a newmap(thisitem).
Next it replaces the name in thisitem to the new name(so that each item has a different name).
Next i put that item in the itemmap for the user. Then I replace the key "items" in Map user with the updated itemmap.
For some reason when I change the name in thisitem, it also changes the name in the template otemp1. This is the problem
Here is where the problem seems to be-
Map thisitem = otemp1;
thisitem.put("name",iname);
items.put(iname,thisitem);
newmap.put("items",items);
users.put(newmap.get("name"),newmap);
otemp1.put("name",itembase);
System.out.println(newmap.get("items").toString());
The code---
if (action.indexOf("giveitem")==0) {
String actionparts[] = action.split("#");
//giveitem#templatename
Map items;
if (newmap.containsKey("items")) {
items = (Map) newmap.get("items");
} else {
items = new HashMap();
if (itemtemplates.containsKey(actionparts[1])) {
Map otemp1;
otemp1 = (Map) itemtemplates.get(actionparts[1]);
if (!otemp1.containsKey("alwayscalled")) {
String itembase = otemp1.get("name").toString();
int x = 1;
String iname = "" + itembase + x;
System.out.println(otemp1.get("name").toString());
//this method is to add a number to the end, such as
//potion5 so that a user doesn't have multiple items with the same name
while ( items.containsKey(iname) ) {
iname = "" + itembase + x;
x = x + 1;
System.out.println("itembase:"+itembase+" iname:"+iname+" x:"+x);
System.out.println("CREATING:"+iname);
Map thisitem = otemp1;
thisitem.put("name",iname);
items.put(iname,thisitem);
newmap.put("items",items);
users.put(newmap.get("name"),newmap);
otemp1.put("name",itembase); //See Note 1
System.out.println(newmap.get("items").toString());
} else {
otemp1.put("name",otemp1.get("alwayscalled"));
items.put(otemp1.get("alwayscalled"),otemp1);
newmap.put("items",items);
users.put(newmap.get("name"),newmap);
//name=gold1~type=gold~takeable=true~amt=100~at=Ashgavh1
//Map itemmap1 = new HashMap();
//itemmap1.put("name",goldbase+z);itemmap1.put("type","gold");
//itemmap1.put("amt","100");
//itemmap1.put("takeable","true");itemmap1.put("at",newmap.get( "location" ));
//objects.put(goldbase+z, itemmap1);
} else {
System.out.println("ERROR:Give Item Failure(No Template FOR "+callingobj+").");
//objects.put(
note1
That was my first attempt to fix it, but that changes the items name in the Map user.get("items") also!! Why? I feel like I'm going to scream. Thanks to anyone who can help me.

thismap and thatmap are not objects. They're just references--pointers to objects.
thatmap = thismap doesn't copy the map. Assignment with = never copies objects in Java. All it does is cause thatmap to point at the same map object that thismap points at. So, naturally, when you change the contents of that object, both references (thismap and thatmap) see the changes.
Look at the APIs for Map.putAll(Map map) or a constructor that takes another Map as an arg. Either one will copy the contents of the map.
But be warned, that's just a shallow copy. The new Map gets its own keys and values, but those are references to the same objects that the original map had.
You can put/remove to one map without it affecting the other, but if you modify the contents of any of the keys (generally a bad idea anyway) or the values, then both Maps will see the changes. If you need to prevent that, you'll have to make copies of each of the key and value objects.

Similar Messages

  • My iphone 5 is 32GB and I keep it backed up in icloud. A recent message suggested that I don't have enough icloud space so I have deleted all back up - it still says no icloud space - help!!! I am not a techy person and this is driving me mad!! :(

    Please can somebody help ....My iphone said not enough icloud space available, so I have delete all back ups and checked what apps are linked to my back up and it is only my photos. Since deleting all current back ups I have tried to back up  again and it still says not enough space when I have 5GB/ 5GB available.
    Can anybody help please? I'd be really greatful as this is driving me mad... why can't things just be simple x

    The problem could be that you don't have enough storage on the device itself, not on iCloud.  Go to Settings>General>Usage to see how much "Storage" you have available on the device.  Farther down the list is the available storage on iCloud.
    Also check:
    Go to Settings>iCloud>Storage & Backups>Manage Storage; there, tap the device you need info on and the resulting screen lists Backup Options with which apps store data on iCloud as well.  Tap Show All Apps to get the complete list of apps and MB used for backup storage.
    A device needs many MB of storage in order to perform a backup to iCloud.
    Also see:  http://support.apple.com/kb/ht4847`

  • Safari cannot open the specified address as OS X does not recognise the internet address starting with "aam". Please please can someone shed some light on this, its driving me MAD!!!

    when i try to download the software onto my mac, i receive a message saying that safari cannot open the specified address as OS X does not recognise the internet address starting with "aam". Please please can someone shed some light on this, its driving me MAD!!!

    Try and download the Desktop Application First before trying to install any other Adobe Product. The link for AAM is Safari looking for Desktop Application in your machine.
    Creative Cloud Help | Creative Cloud for desktop

  • Siri keeps cutting in and out when I'm using my iPad3 and I don't even use Siri. This Is driving me mad. Can someone find it in their heart to help me.thanks,Michael.

    SIri keeps cutting in and out when I'm using my iPad 3 and it's driving me mad.
    i Don't even use Siri and know I can turn it off but don't want to.
    could someone please find it in their heart to try and help me with this issue.
    kind regards
    michael.

    If you have your iPad connected to the charger it could be the new "Hey Siri" feature that allows you to use Siri without pressing the home button. Do you have a radio on or are people talking ? Go to settings> general> Siri> Allow"Hey Siri"> off.

  • I have an acer aspire TC-220 WIN 10 I cannot get my pc to boot from CD drive this is driving me mad

    HiI have an Acer Aspire TC-220 desktop 16gb ram 3 tb hard drive AMD A-10-7800 RUNNING WINDOWS 10This is driving me mad I cannot for the life of me get my PC to boot from the CD drive I believe you have to change the drive in Bios from the UEFI drive to a legacy drive there is no provision to do this I have tried everything in the Bios nothing works I have set the 1st priority as the cd drive please can you helpmany thanks

    ecneps1948,
    Is the optical drive being seen and is usable in Windows itself?

  • How do i filter recordsets using session variable???  Please help this is driving me mad...!!

    I am having the same problem as user "Gabe the animator" in a post sent in 2007.
    "my recordset that drives a dynamic table won't filter results based on a session variable. I know session variables are working because I have the session variable echo on the page (dragged-n-dropped my session variable from the Bindings panel to my page), and that works fine. So why can't I filter my recordset with the same session variable???"
    here is the code:
    <?php require_once('Connections/mockconn.php'); ?>
    <?php
    session_start();
    ?>
    <?php
    if (!function_exists("GetSQLValueString")) {
    function GetSQLValueString($theValue, $theType, $theDefinedValue = "", $theNotDefinedValue = "")
      if (PHP_VERSION < 6) {
        $theValue = get_magic_quotes_gpc() ? stripslashes($theValue) : $theValue;
      $theValue = function_exists("mysql_real_escape_string") ? mysql_real_escape_string($theValue) : mysql_escape_string($theValue);
      switch ($theType) {
        case "text":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;   
        case "long":
        case "int":
          $theValue = ($theValue != "") ? intval($theValue) : "NULL";
          break;
        case "double":
          $theValue = ($theValue != "") ? doubleval($theValue) : "NULL";
          break;
        case "date":
          $theValue = ($theValue != "") ? "'" . $theValue . "'" : "NULL";
          break;
        case "defined":
          $theValue = ($theValue != "") ? $theDefinedValue : $theNotDefinedValue;
          break;
      return $theValue;
    if(!session_id()){
    session_start();
    $colname_info = "-1";
    if (isset($_SESSION['email'])) {
      $colname_info = $_SESSION['email'];
    mysql_select_db($database_mockconn, $mockconn);
    $query_info = sprintf("SELECT name, last_name, email, password FROM registration WHERE email = %s", GetSQLValueString($colname_info, "text"));
    $info = mysql_query($query_info, $mockconn) or die(mysql_error());
    $row_info = mysql_fetch_assoc($info);
    $totalRows_info = mysql_num_rows($info);
    ?>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>home</title>
    </head>
    <body>
    <div id="info">hello <?php echo $row_info['']; ?></div>
    <?php
    if (isset($_SESSION['email'])) {
              echo 'your email: '.' '. $_SESSION['email'] .' '.'good job';}
                        ?>
    </body>
    </html>
    PLEASE PLEASE HELP.... I have been at this for day's...
    how do I get the record set to filter based on the value of the session variable

    Sorry I forgot to mension the error I am getting?
    ( ! ) Notice: Undefined index: in C:\wamp\www\mock\home.php on line 59
    Call Stack
    Time
    Memory
    Function
    Location
    1
    0.0093
    389024
    {main}( )
    ..\home.php:0
    Why is this error coming up?

  • Please help this is driving me mad!!!!!!!

    Guys,
    I really need your help!!!
    when my computer is running at stock speeds or overclocked to 2.4ghz as per my sig my raid array works fine and boots into windows. But as soon as i over clock over 2.4ghz my raid array dissapears, i know my chip can go higher because before i moved to a raid array i ran it at 2.5 ghz 250x10 HTT @ 4, mem on a 166 divider with no problems.   BTW i have updated to sygar's 1.9 modded bios (that rocks mate gj) The momory is 2x512 of corsair value select which runs in my machine at 2.5 3 3 6 think its rated at 3 3 3 8 though.
    Can anyone help me i want the most from my chip but i also want to use my raid array.
    Please help as i am sure this should be possible

    Well....  more than likely when you are overclocking, you are pushing your PCI bus speeds way out of whack.  Many controller type cards (built in or not) don't like having the PCI bus more than 3-4Mhz faster than 33Mhz.  Use the clockgen utility or whatever program you like to see what your PCI bus speed is at your current settings, then look and see what it would be at 2.5Ghz.
    Good luck!
    Merrell

  • HT201407 iMessage "unable to activate, please try again".    Whenever I go to activate imessage is says waiting for activation and then about 5 mins later says activation unsuccessful, please try again. This is driving me mad! It's been 2 days now!!

    Lease help me to activate imessage. I now have iPhone 5s and can't activate imessage. I've tried all the normal stuff but nothing is working

    i had this problem with my iphone 5c and sorted it contacting apple.
    have you tried resetting the network settings? it doesnt delete anything and should work. if it then changes to "waiting for activation", instead of the error message, then leave it for 24 hours (leaving your phone and wifi on) and it should activate and work.
    for me, this didnt work after 24 hours. i then backed up my phone and reset it and set it up as a new iphone then restored it using my backup from the computer and it now works

  • Trying to change my icloud apple id to match my itunes apple id. i lost my password and i don't have access to very old email address it keeps wanting to send my password to. please help!!! this is driving me mad!

    I've been trying to change my icloud apple ID to match my itunes apple ID so i can manage my storage.  I haven't turned on the cloud since i got the phone.  I've forgotten my password for the icloud apple ID and i do not have access to the OLD OLD OLD email address its trying to send my password to.  Can someone please tell me how i can make this change without knowing my old password???  Can't manage storage or buy more without knowing the old apple id for icloud. 

    Hello, nikkihutchins10.  
    Thank you for visiting Apple Support Communities.  
    I understand that you are attempting to reset your Apple ID password.  I would try resetting your password via answering the security questions instead of sending the email.  The first article will walk you through that process.  If you are still unable to reset your Apple ID password, see the steps in the second article.  
    If you forgot your Apple ID password 
    Contact Apple for help with Apple ID account security
    Cheers, 
    Jason H.  

  • Help please? This is driving me MAD!!!

    I have a gui and in it is a JLabel. The text for the JLabel is gained from a method class to another class.
    The information is constantly changing and the JLabel needs to keep up to date. At the moment, i have a JButton that calls the method and then that updates the GUI.
    I dont want to have to press the button. How can i do it so that it updates automatically, say every 5 seconds. Is there something i can but in actionPerformed???
    Everything i have tried has reulted in the GUI not showing anything at all!
    PLEASE PLEASE PLEASE PLEASE HELP!!!!

    please its for a very small bit of my dissertation.
    I would read it, but im in hell trying to write the
    technical report that has to go with my system.I thought your dissertation was a defense of knowledge you claim to gain during an investigation. And you need this information for that defense. If it is just given to you, how can you claim to have learned it?
    � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • This Is Driving Me Mad...

    I dont know too much about computers, but can any one help me here..can you tell me why my internet either quits or crashes, especialy when I am brousing E.bay.
    I am on 10.2.4.
    Plain laymans terms is better for me with any reply's.
    Thanks for any imput you may have to offer.
    i Mac   Mac OS X (10.2.x)  

    d.link..dsl300g
    I dont lose the connection, cos I can recall again. And it happens with safari and explorer..
    I am sorry I have been away for a few days, and I was unable to respond sooner than this I hope you can still help.
    Thanks
    Jackie..

  • PleASE PLEASE HELP THIS IS DRIVING ME MAD

    This should be so simple, all I have is a java class which I need is used
    to ax a oracle db and returns a recordset.
    1st q do I need to reg this class or does web logic look in a specific area
    and reg them automatically.
    2nd question I have a jsp page that simply imports java.sql when access the
    page I get an error
    stating it does not recognise Recordset, as thou it has not imported the
    Java.sql??????????????????

    "Manuel" <[email protected]> wrote:
    This should be so simple, all I have is a java class which I need is used
    to ax a oracle db and returns a recordset.
    1st q do I need to reg this class or does web logic look in a specific area
    and reg them automatically.
    2nd question I have a jsp page that simply imports java.sql when access the
    page I get an error
    stating it does not recognise Recordset, as thou it has not imported the
    Java.sql??????????????????
    Recordset is new to me.
    I generally use ResultSet to get the data.
    import java.sql.ResultSet;

  • Installed iOS 5.0 and wifi will not stay up on my phone and only way to reset it is power reset my wireless router? Please help this is driving me mad!!!!

    Have iPhone 4 just installed iOS 5.0.1 and wifi will not
    Stay up on my phone even though router is 10 feet away
    And cannot recover wifi except by resetting router never. Asks for
    PW etc? Plus battery life is reduced on this iOS?

    Try a SMC reset:
    http://support.apple.com/kb/HT3964
    Try a NVRAM/PRAM reset:
    http://support.apple.com/kb/HT1379
    If no joy, take it back, and take full advantage of your warranty!
    You also may want to let Apple know about this as well:
    http://www.apple.com/feedback//
    I wish you the best

  • This is driving me crazy--new problem...

    For some reason, I can't load songs into my iPod. I have some recently imported CDs in iTunes. Anyway, I drag them from a folder into my iPod, and iTunes will say the songs are copying into the iPod, but when I look for the songs, neither the song/artist I wanted to load is there. The total # of songs displayed at the bottom does not increase, nor does the total GB usage.

    Okay.
    About 70 million iPods have been sold. Trust me, out of those 70 million iPods, very few have problems.
    See if recreating the iTunes library helps (it won't affect your music)...
    How to recreate your iTunes Library
    Then try restoring your iPod...
    Restore iPod
    See if it works now.
    btabz

  • Sumproduct in Numbers driving me mad!

    Hi All, looking for someone to tell me im being really silly here, this is driving me mad!!!
    So, I've been trying to impliment the following sumproduct in Numbers, here are my two tables, both on the same sheet. This is a test workbook ive been playing around with...
    Table 1
    A
    B
    C
    D
    1
    Make
    Month
    Sold
    Price
    2
    Ford
    June
    1
    £100.00
    3
    Ford
    June
    3
    £200.00
    4
    Ford
    May
    4
    £300.00
    Table 2
    A
    B
    C
    1
    Make
    Month
    Total
    2
    Ford
    June
    ? = the following formula: =SUMPRODUCT((Table 1::A2:A4=A2)×(Table 1::B2:B4=B2)×(Table 1::C2:C4))
    Numbers comes to the value of 1. Interestingly enough, when i export to Excel, the answer magically becomes 4. Im a bit of an Excel geek so would like to think i know my stuff, but am trying to switch to Numbers for simple stuff because I like being able to view sheets on my iPhone, but this is driving me mad!
    Any advice you can give would be brilliant!
    Thanks in advanced,
    Dave.

    I would add a new column to the top table (in my example I am calling it "Sales Data"):
    The new column is called "Extended Amount" and is the product of the number sold and the price
    E2=C2×D2
    this is shorthand for select cell E2, then type ( or copy and paste from  here) the formula:
    =C2×D2
    select E2 and fill down
    Now in the the summary table (the lower table):
    C2=SUMIF(Sales Data::B, B2,Sales Data::E)
    this sums the extended amount for all months that match what you enter in cell B2 in the month column of the table "Sales Data"

Maybe you are looking for

  • Insert 2D Array in 2D Array

    okay... I have the following case: I have a 2 inputs which are both arrays of numeric datatypes. Both arrays can have a different number of rows, only the amount of columns is the same. For example: array1: [[1,2]] array2: [[3,4],[5,6]] output array:

  • Ken Burns Deffect

    Has anyone had this happen? When you use the "Ken Burns Effect", (which would be pretty cool if it worked) Mac gives you the option to direct the movement of the effect. Then when you go to play it negates the commands you just gave it and or it won'

  • Textual Data in Fact Table

    Hi I would like to understand what can be the probable issues which I may face in OBIEE 11g if my fact table has non- numeric data (textual data) like type or rejection reason in the fact table. I am new to the team and the data warehouse consist of

  • Error in JCheckBox itemStateChanged Method

    Sample Code: import java.awt.*; import javax.swing.*; import java.awt.event.*; public class JCheckBoxTest extends JPanel implements ItemListener {      JCheckBox one, two, three, four;      JLabel LabOne, LabTwo, LabThree, LabFour;      JTextArea tex

  • Is it possible to AirPlay an ipad screen(keynote) to multiple appletvs on the same wifi?

    Is it possible to AirPlay an ipad screen(keynote) to multiple appletvs on the same wifi?