Results 1 to 5 of 5
- 11-04-2010, 09:37 AM #1
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 10
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 } } } }
gejeLast edited by mine0926; 11-04-2010 at 09:41 AM.
- 11-04-2010, 01:55 PM #2
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?
- 11-04-2010, 05:29 PM #3
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
- 11-05-2010, 02:41 AM #4
Senior Member
- Join Date
- Apr 2010
- Location
- Philippines
- Posts
- 580
- Rep Power
- 10
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 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,
gejeLast edited by mine0926; 11-05-2010 at 06:12 AM. Reason: EDIT: past to pass
- 11-06-2010, 01:19 PM #5
- Join Date
- Jul 2007
- Location
- Colombo, Sri Lanka
- Posts
- 11,370
- Blog Entries
- 1
- Rep Power
- 25
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
-
How to delete a JLabel by using the keyboard 'delete' key?
By Suren in forum AWT / SwingReplies: 2Last Post: 04-20-2009, 09:00 AM -
Delete Student from Array Help
By kazit in forum New To JavaReplies: 4Last Post: 02-25-2009, 03:42 AM -
help to delete
By andykots in forum Advanced JavaReplies: 1Last Post: 01-29-2009, 09:29 AM -
help array set null or delete
By chkm8 in forum New To JavaReplies: 1Last Post: 01-19-2009, 09:15 PM -
Delete
By Sarinam in forum New To JavaReplies: 6Last Post: 07-23-2008, 12:09 PM
Bookmarks