Results 1 to 5 of 5
  1. #1
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default How can I delete a row in a 2d Array.

    Hi, I have to check for a duplicate entry and if found duplicate entry I have to add its quantity and delete the latter then

    I use two(2) for loop to do this.
    Java Code:
     private void checkDupEntry(Object[][] mat_toUse)
     {   Object matID_ToCheck = "";
         int qty = 0;
    
        for(int intMat = 0; intMat < mat_toUse.length; intMat++)
        {   matID_ToCheck = mat_toUse[intMat][0];
            qty = Integer.parseInt(mat_toUse[intMat][1].toString());
    
            for(int intCheck = intMat; intCheck < mat_toUse.length; intCheck++)
            {   if(matID_ToCheck.equals(mat_toUse[intCheck][0]))
                {   qty += Integer.parseInt(mat_toUse[intCheck][1].toString());
                    //Delete 2D array
                }
    
            }
        }
     }
    Thanks,
    geje
    Last edited by mine0926; 11-04-2010 at 08:41 AM.

  2. #2
    KevinWorkman's Avatar
    KevinWorkman is offline Crazy Cat Lady
    Join Date
    Oct 2010
    Location
    Washington, DC
    Posts
    3,960
    Rep Power
    8

    Default

    What have you tried? What do you want to happen when you delete that row? Do you want the other contents to shift, or should that row just be empty? Do you have to use arrays, or can you use Lists?

  3. #3
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

  4. #4
    mine0926 is offline Senior Member
    Join Date
    Apr 2010
    Location
    Philippines
    Posts
    580
    Rep Power
    5

    Default

    Here is what I tried so far:
    Java Code:
    private void checkDupEntry()
    {        Object[][] arrMat = {{"A001", 1}, {"A001", 1}, {"A001", 1},
            {"A002", 1}, {"A002", 1}, {"A003", 1}};
    
            Object matToCheck = "";
            
            int qty = 0;
            for(int intMat = 0; intMat < arrMat.length; intMat++)
            {   //Loop again to check for the same Material
                if(arrMat[intMat][0] != null )
                {   matToCheck = arrMat[intMat][0];
                    qty = Integer.parseInt(arrMat[intMat][1].toString());
    
                    //intMat + 1 so that it will not check itself
                    for(int intCheck = intMat + 1; intCheck < arrMat.length; intCheck++)
                    {   if( matToCheck.equals(arrMat[intCheck][0])   )
                        {   System.out.println(intCheck);
                            qty += Integer.parseInt(arrMat[intCheck][1].toString());
                            arrMat[intCheck][0] = null;
                            arrMat[intCheck][1] = null;
                        }
                    }
    
                    arrMat[intMat][1] = qty;
    
                }
            }
            Object[] colNames = {"MATERIAL", "QTY"};
            DefaultTableModel dtm = new DefaultTableModel(arrMat, colNames);
            jTable1.setModel(dtm);
    }
    I just set the value of duplicate values to null. These will not delete the row so do I have to use another array and us if else statement to eliminate arrays with null value? Is this the only way to check and delete the Row in an array?

    I also try ArrayList and Lists but I cannot simply pass arrMat. I think ArrayList or Lists is asking me for a Collection and not an Object[][].

    Thanks,
    geje
    Last edited by mine0926; 11-05-2010 at 05:12 AM. Reason: EDIT: past to pass

  5. #5
    Eranga's Avatar
    Eranga is offline Moderator
    Join Date
    Jul 2007
    Location
    Colombo, Sri Lanka
    Posts
    11,372
    Blog Entries
    1
    Rep Power
    20

    Default

    null not leads the idea of empty. Even you delete what you've really done was set the value to null. If you want to do this with arrays, yes on each item deletion you have to define a new array and assign values with the new size of the array.

    Actually with arrays you can do that, once you remove an element simply adjust the size and ordered rest of the elements in the array.

Similar Threads

  1. Replies: 2
    Last Post: 04-20-2009, 08:00 AM
  2. Delete Student from Array Help
    By kazit in forum New To Java
    Replies: 4
    Last Post: 02-25-2009, 02:42 AM
  3. help to delete
    By andykots in forum Advanced Java
    Replies: 1
    Last Post: 01-29-2009, 08:29 AM
  4. help array set null or delete
    By chkm8 in forum New To Java
    Replies: 1
    Last Post: 01-19-2009, 08:15 PM
  5. Delete
    By Sarinam in forum New To Java
    Replies: 6
    Last Post: 07-23-2008, 11:09 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •