Having Problems with Hashmap

Hi, I'm currently creating a sparsematrix for my assignment using a hash map however i've come across a problem with how i can set an element and returning using key objects
My setElement method is
public void setElement(int nrow, int ncol, float value) throws
MatrixException {
data.put(new SparseNode(nrow,ncol), value);
and my get element is
public float getElement(int row, int col) throws MatrixException {
SparseNode node = new SparseNode(row, col);
          if (!data.containsKey(node)) {
          return 0;
return (Float) data.get(node);
where data is the map and node would be the key. The problem comes when i set the value where i give a key and this is set into the hashmap. unfortunatly the key is lost when the method exits so using the getElement method by creating a sparsenode with same characteristics as the original key won't work simply because it's not the original key. Is there another way to get around this? or some way to store the key.

ok well i changed to to not include the SparseNode. Now it works but very very slow. I also noticed another post with a user with the same problem but it didn't really explain how to fix it properly. Any suggestions to make this code faster?
import java.io.*;
import java.util.*;
public class SparseMatrix implements Matrix{
    private HashMap data; // non zero elements
     private int rows;
     private int columns;
    public SparseMatrix(int row, int columns) {
        data = new HashMap(row*columns);
        this.rows=row;
        this.columns=columns;
    public float getElement(int row, int col) throws MatrixException {
            if (row < 0 || col < 0 || row > this.getNumRows() || col > this.getNumCols()) {
            throw new MatrixException("Row or Column Number Beyond Dimension");
                 if (!data.containsKey((row*this.getNumCols()+col))) {
                    return 0;
        return (Float) data.get((row*this.getNumCols()+col));
    public void setElement(int nrow, int ncol, float value) throws MatrixException {
//        if (row > this.row || col > this.col) {
//           throw new MatrixException("Matrix index out of range");
        data.put((nrow*this.getNumCols()+ncol), new Float(value));
    public boolean isZero() {
        return data.isEmpty();
    // return the total number of rows in the matrix
   public int getNumRows(){
        return rows;
   // return the total number of rows in the matrix
   public int getNumCols(){
        return this.columns;
   // transpose matrix and return result as new matrix
    public Matrix transpose() {
        SparseMatrix a = new SparseMatrix(this.getNumCols(), this.getNumRows());
        for (int i =1 ; i<=this.getNumRows();i++) {
            for (int j = 1;j<=this.getNumCols();j++){
                  float value = getElement(i,j);
                 a.setElement(j, i, value );
        return a;
    * Subtracts a matrix to the current matrix and returns result as new matrix
    * @param a The Matrix to be subtracted
    * @return Returns the new matrix
   public Matrix subtract(Matrix a) throws MatrixException{
             if (this.getNumRows() != a.getNumRows() || this.getNumCols() != a.getNumCols())
                throw new MatrixException("Subtraction Cannot be Done due to Matrix Dimension MisMatch");
            if (a.isZero())
                return this;
                SparseMatrix ResultMatrix = new SparseMatrix(this.getNumRows(), this.getNumCols());
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= columns; j++) {          
                 float value = a.getElement(i,j);
                 float result = this.getElement(i,j) - value;
                 if (result < 0 || result > 0) {
                     ResultMatrix.setElement(i,j,result);
             return ResultMatrix;
    // add matrix and return result as new matrix
    public Matrix add(Matrix a) throws MatrixException {
            if (this.getNumRows()!= a.getNumRows() || this.getNumCols()!= a.getNumCols())
            throw new MatrixException("Addition Cannot be Done due to Matrix Dimension MisMatch");
        if (this.isZero())
            return a;
        if (a.isZero())
            return this;
            SparseMatrix ResultMatrix = new SparseMatrix(this.getNumRows(), this.getNumCols());
        for (int i = 1; i <= rows; i++) {
            for (int j = 1; j <= columns; j++) {          
                 float value = a.getElement(i,j);
                 float result = this.getElement(i,j) + value;
                 if (result < 0 || result > 0) {
                     ResultMatrix.setElement(i,j,result);
             return ResultMatrix;
// multiply matrix and return result as new matrix
     public Matrix multiply(Matrix a) throws MatrixException {
                          if (this.getNumCols() != a.getNumRows())
            throw new MatrixException("Multiplication Cannot be Done due to Matrix Dimension MisMatch");
        if (this.isZero() || a.isZero())
            SparseMatrix Temp = new SparseMatrix(this.getNumRows(), a.getNumCols());
            return Temp;
         SparseMatrix ResultMatrix = new SparseMatrix(this.getNumRows(), this.getNumCols());
          for(int i=1;i<=this.getNumRows();i++)
               for(int j=1;j<=a.getNumCols();j++)
                              float Result =0;
                              for(int k=1;k<a.getNumCols();k++)
                                   Result = Result + this.getElement(i,k)*a.getElement(k,j);
                              ResultMatrix.setElement(i,j,Result);
          return ResultMatrix;
     // print matrix in the format:
   // a11 a12 ... a1m
   // an1 an2 ... anm
    public void print(PrintStream out) {
         //System.out.println(data.toString() +"\n=====================");
        for (int i = 1; i <= this.getNumRows(); i++) {
            for (int j = 1; j <= this.getNumCols(); j++) {
                float aData = this.getElement(i, j);
                out.print(" " + aData + "");
            out.println();
}

Similar Messages

  • Having problems with Bridge shutting down after saving photo in PS Cs5,,,,,,,,,  Also when i attempt to update either PS or bridge get an error message in Adobe application manger "Error loading updater workflow"

    Having problems with Bridge shutting down after saving photo in PS Cs5,,,,,,,,,  Also when i attempt to update either PS or bridge get an error message in Adobe application manger "Error loading updater workflow"

    Sorry for the late reply. My email firewall has become a little over zealous & sent a lot of my emails straight to my junk email folder, so I have only just now discovered your reply in my junk mail folder.
    The only "don't open files exceeding xxx megabytes" instruction I can find in my Prefs, is in the Bridge Prefs for Thumbnails, & mine is set at 1000mb. The biggest files I handle are bigger than 200mb so I should be able to open a few, not just one.
    However, this doesn't explain why I can open a psd format file of 180mb, close it, but then can't open a RAW format file of only 26mb immediately after.
    I can open the RAW file only if I restart my computer - very annoying!
    However, thanks for the advice about the video card & memory.
    So, I'm still stuck as to what the issue is.

  • Is anyone having problems with the battery heating up and draining the power?  Mine has been doing this for about 2 months now.

    Is anyone having problems with the battery heating up and draining the power?  Mine has been doing this for about 2 months now.

    This is a major problem with Lollipop, but yo said it started 2 months ago.   Several people have fixed problem by removing FaceBook And Face book messenger and then re loading the apps again.   You might want to clear your cache before reloading.  Not sure this is your problem, but worth a try.  Good Luck

  • I am having problems with creating my apple id. On the phone it is advising me that the ID is not created and then when i do it online it says that it has been created. Can you advise me on what to do please?

    I am having problems with creating my apple id. On the phone it is advising me that the ID is not created and then when i do it online it says that it has been created. Can you advise me on what to do please?

    Apple ID FAQ   http://support.apple.com/kb/HE37

  • HT1657 I am having problems with sound in my rental movies

    We rent movies all the time but lately we are having problems with sound for our movie rentals.  We watch the first half hour without a problem than the voice is choppy all the time.  Sometimes it is good for five minutes than noting for a while.  We  have tried everything we could think of with no avail.  I could see one movie but the last 5 or so have been the same.  Done trying we need to fix this!  Help!  Any ideas?

    First try resetting your iPad by simultaneously pressing and holding the Home and Sleep/Wake buttons until you see the Apple Logo. This can take up to 15 seconds so be patient and don't release the buttons until the logo appears. Try again to see if the problem persists.
    If you still have the same issue, it's possible that something is lodged in the earphone jack. This can cause the iPad to think earphones are plugged in when they are not. Use a vacuum to try to suck any dust, lint or other foreign matter from the jack. If that still doesn't work, try CAREULLY using a toothpick.

  • Hi Can anyone help. I'm having problem with FaceTime on my IPAD .It connects but cuts out Simolarly when someone calls me it connects , then cuts out ThanksBrian

    Hi Having problem with FACETIME on my IPAD Connects to call but then cuts out , similarly calls in connect &amp; cut out Can someone help. Brian

    My ipad is doing the same thing to it won't let me except a incoming call on Facebook and I don't know what to do

  • I am using my Ipad on a remote craftshow site and am having problems with battery lasting 8 hrs. Can I use a 12v rv battery to recharge it or atleast maintain the charg over longer periods of time? I have a dc adapter. thanks

    I am using my Ipad on a remote craftshow site and am having problems with battery lasting 8 hrs. Can I use a 12v rv battery to recharge it or at least maintain the charge over longer periods of time? I have a dc adapter. thanks

    You should be able to use the battery if you get a car type charger which recharges mobile devices.
    The full size iPads use a 10-12W charger, whereas the mini uses a 5W charger. Many car chargers only supply 5W. But at 5W, the car charger should give you some extra time on your iPad. It would be best to use a 10-12W car charger.
     Cheers, Tom

  • I am having problems with my music collection, after burning all of my music to the computer I organized it into folders and placed it in the iTunes Folder. Starting on a new computer now. I can get my music from old computer?

    I am having problems with my music collection, after burning all of my music to the computer I organized it into folders and placed it in the iTunes Folder. As of now i realize this was a mistake. Starting on a new computer now. I cant get my music to transfer from the itunes folder on the old computer? I keep getting error "Can't Read from the source file or disk." Whenever I got to folder properties its always checked read only. But when i play the song it works just fine. Im sure this is just because I added music into itunes folder but is there anyway to fix it?

    WMA files are 'window media audio' files, which is a Microsoft format. If you want to add them to your iTunes library on your Mac then you will need to convert them into a compatible format first. If you still have your windows machine then iTunes for Windows can convert them from WMA to MP3 format : https://discussions.apple.com/message/24158701#24158701
    Or try a search for, for example, 'convert wma to mp3' to find programs to convert them.

  • I am having problem with MDV Studio.

    Hello happy guys, whatzup? Guess all ya 'r doing fine? Right!
    I am using Dreamweaver from Macromedia Studio 2004. I am having problem with Inserting Record into Access database using the forms I create on my .asp document. I am using IIS and Javascript as my Document Technology. The error I usually have is:
    "Technical Information (for support personnel)
    Error Type:
    Microsoft OLE DB Provider for ODBC Drivers (0x80004005)
    [Microsoft][ODBC Microsoft Access Driver] Operation must use an updateable query.
    /site_files/user_regform.asp, line 78
    Browser Type:
    Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US) AppleWebKit/530.5 (KHTML, like Gecko) Chrome/2.0.172.28 Safari/530.5
    Page:
    POST 41 bytes to /site_files/user_regform.asp
    POST Data:
    uname=Honesty&pword=admin&MM_insert=form1
    Time:
    Tuesday, June 02, 2009, 1:49:39 PM"
    Please I need assistance on how to solve this problem. Thank you.
    Similarly, I don't really know how to write sql strings - I just give commands and Dreamweaver will generate the sql strings. Yet, I still observe errors. But I think, for the sql aspect, I'm undergoing a tutorial note that I downloaded on the internet.
    Please help me to solve this error problem at the moment. The sql knowledge is not the problem. I can get that at my leisure.
    Thank you.
    Eyo Eyo (Nigeria).

    It sounds like you do not have an internet connection (wifi arcs in the upper left does not mean you have a connection) since Safari does not work also.. Try the following:
    - Reset the iPod. Nothing will be lost.
    Reset iPod touch:  Hold down the On/Off button and the Home button at the same time for at
    least ten seconds, until the Apple logo appears.
    - Power off and then back on the router
    - Reset networks settings: Settings>General>Reset>Reset Network Settings
    - iPhone and iPod touch: Troubleshooting Wi-Fi networks and connections

  • I am having problems with my itunes, when plugging my iphone or ipod to the laptop - i can see iphone/ipod in the top right corner of itunes but when i click this to the see what is on theis iphone/ipod the option is no longer there, this was working

    I am having problems with the new itunes set up ........
    when i plug in my iphone or ipod touch it shows in the top right hand corner of itunes
    BUT when i click on the shown iphone/ipod the icon its no longer there, i was useing this fine yesterday and it was showing yesterday what is on this ipod and would allow me to add/remove music, but now this option is no longer availlable - i have added a couple of albums to my itunes i can listen to the music from the laptop but cannot sync the ipods to trancefer these albums to the handheld devices -
    i have been trying to sort this all day with no luck not sure if there is a problems with itunes or is it something i am doing ????
    HELP !!!!!!!!!!!!

    BOB we need you urgently
    Perhaps Bob will see this thread, he is the professor in these cases.
    You do not use a WEP password but wpa/wpa2 on router and equipment.
    All equipment connects to WiFi, but for your iPad.
    Even with all other equipment WiFi off, you cannot connect your iPad.
    Your iPad connects to other WiFi nets correctly.
    All resets etc done on iPad and Router.
    Still I am not sure it is your router for sure, frustrating...
    I want to test 2 other things:
    1. in your iPad, set everything OFF in Settings/Cellular, turn off/then/on your WiFi, and see whether it will still not connect...
    2. If that does not do it: disconnect the router as well as the modem from the power, for 15 seconds, then reconnect the modem, let it fully start, then reconnect the routet let it fully start, turn of your WiFi in iPad, then on, and see whether it will connect.

  • I am having problem with me Iphone4, looks like the mic of my phone is not working. no one can hear me if I make a call to them

    Can any one please help me I am having problem with me Iphone4, looks like the mic of my phone is not working. no one can hear me if I make a call to them. I have tried all restoring options but no use. please help me

    The mic on my iPhone4 has just quit also. Similar symptoms to yours in that people I'm talking to hear only static or my voice very faintly. The voice/memo recorder also only really records static.
    It happened suddenly and seemed to get better after a day or two but then went completely after another day.
    I'm returning it to Vodafone New Zealand shortly but will have to wait 5-10 days for a replacement. There are NO Apple accredited means to speed this procedure up in NZ.
    We don't even have a single physical Apple Store where you could walk in and talk to a Genius.

  • I am having problems with the Preview application.  When I try to use it to open a pdf document it gets hung-up and I have to select force quit to close it.   Any ideas on how to resolve this problem?  Thanks for any help

    I am having problems with the Preview application.  When I try to use it to open a pdf document it gets hung-up and I have to select force quit to close it.   Any ideas on how to resolve this problem?  Thanks for any help

    Can you open the Preview program without loading a file, like by itself?
    If it doesn't load then I suspect a corrupt Preview preference file.
    Deleting the System Preference or other .plist file
    Can you open other files with Preview, like jpg's and images?
    How about other PDFs? or is it just that one you have downloaded?
    Run through this list of fixes
    Step by Step to fix your Mac

  • I'm having problems with my app, plants vs. zombies 2. It'll open and stay open for a few seconds then shut down. Help please.

    I'm having problems with my app, plants vs. zombies 2. It'll open and stay open for a few seconds then shut down. Help please.

    Check to see if there are Updates available... If already up to date...
    Try This...
    Close All Open Apps... Sign Out of your Account... Perform a Reset... Try again...
    Reset  ( No Data will be Lost )
    Press and Hold the Sleep/Wake Button and the Home Button at the Same Time...
    Wait for the Apple logo to Appear...
    Usually takes about 15 - 20 Seconds... ( But can take Longer...)
    Release the Buttons...

  • I'm having problems with 8.1 and Continuity / Handoff. It will work fine for web pages, etc. but in email when I try to do it between by iPhone 5s running 8.1 and my Macbook Pro running Yosemite I consistently get an error.

    I'm having problems with 8.1 and Continuity / Handoff. It will work fine for web pages, etc. but in email when I try to do it between by iPhone 5s running 8.1 and my Macbook Pro running Yosemite I consistently get an error. "Failed to Continue Activity" Cocoa Error 4609.  Handoff is working for phone calls and text messages. By email just crashes each time. It was also doing it under 8.0.2.  My iPhone and iPad handle this fine. It's only the MacBook to the iPhone that fails, and only on email.

    Handoff Continuity Troubleshooting

  • I am having problems with mail after upgrading to mountain lion. Is there a way to go back to lion?

    I am having problems with mail after upgrading to mountain lion. Is there a way to go back to lion?

    It would always be easier to fix the Mail problems.

Maybe you are looking for

  • Raw files with CS5 photoshop and Nikon D3300

    I currently have CS5 suite, I know it's old..but its what I have to work with.  I just purchased the Nikon D3300 and cannot open the raw files.  I installed the 8.4 dng converter, but obviously that does not work.  i tried to uninstall it and install

  • C309a (Duplex Printing): the first two pages don't "duplex"

    Hi, I've got a little annoyance here: I love the duplex printing feature, but the first two pages don't "duplex!" The first page prints fine but doesn't get retracted back inside, and then the second page prints upside-down on a new sheet of paper. 

  • ITunes taking too much space

    I'm nuit sure but I think iTunes is taking a lot of space from my Computer for backups. Is there any way I could remove the useless purchases I made?

  • Company description in DTA file for automatic payment

    Hi all I have changed the description of my company in customizing (T001). When i generate the DTA file after the payment generation (F110), the company description is still the old name. Where i must change? thanks Davide

  • Integration Broker Practice

    Dear All, I am very new to Integrations and read, watched as much as I can on web services J If you have time, I will be very happy if you can look at my scenerio and share your comments with me for doing my job in a right way. Thanks for all your he