I have double dimensional array of dimensions 720x90. Let's denote rows by R and C as columns.
R1 = {C1,...,C90}
....
R720 = {C1,...C90}
Now, I want to see if any of the data in any of the rows appears anywhere else in any other rows. For instance, lets say the data in row 470 and column 67 is a duplicate of row 672 and column 34. In that case, I want to remove both row 470 and row 672 from the data set and continue checking. After I have checked all the rows, I want to print just the index of the rows that have survived. I have written a brute-force method of this. However, when I run this code, it never returns and I am not able to diagnose why. Also, is there a more efficient way to do this?
//check all the subsets of the interleaved data
public static int checkSubsets(String[][] subsets){
List subset = new ArrayList();
for(int i = 0; i< 720; i++){
for(int j = 0; j < 90; j++)
subset.add(subsets[i][j]);
}
Object duplicate;
Iterator itr = subset.iterator();
while(itr.hasNext()){
duplicate = itr.next();
while(itr.hasNext()){
subset.remove(duplicate);
itr=subset.iterator(); //to avoid concurrent modification
itr.next();
}
}
return subset.size();
}
Clarifications: Lets say I am iterating through looking at each value in the matrix. I take the first value in R1 C1 (row 1 - column 1). I find that these values are found somewhere in the 12, 346,123, 356 row. Then I remove all those rows from the matrix. So now the matrix is 5 rows smaller. I stop checking row 1 now and move onto row 2. I continue checking, skipping over row 12, 346, 123, and 356. Hence, I am after a row that is unique (has 90 values that are all unique).
I am not sure what the code you wrote has to do with the requirement, I will give you the approach of the answer yet you have to try it yourself first.
it is clear that you need to iterate on each row to check for possible duplicates yet this will cause a performance failure , you can overcome this with a smiple use of HashMap, first store each entry in the map , the key will be the value of the node of the array, and the value should be the coordinates of this node.
When iterating over the array for each row you should find the y coordinates from the map which is common between all nodes of the row, so duplicate rows detected.
In order to avoid keep checking the already removed rows try to store all the rows to be deleted and remove them once you are done, you can use Set to store them to avoid duplicate.
Good luck with the implemenation.
The algorithm is almost there, but helpfull data-structures are missing.
To add a bit of spice I used Java 8 somewhat.
As you did one can collect the values to check for duplicates.
However one needs to remember the first row of that value, as only there it is still unknown whether a duplicate exists.
public static int checkSubsets(String[][] subsets) {
// The results.
final Set<Integer> duplicateRows = new HashSet<>();
// From the first occurrence of a duplicate value we do not know it yet,
// so need to remember.
final Map<String, Integer> firstRowOfValue = new HashMap<>();
for (int i = 0; i < subsets.length; ++i) {
for (int j = 0; j < subsets[i].length; ++j) {
final String value = subsets[i][j];
Integer oldRow = firstRowOfValue.putIfAbsent(value, i);
if (oldRow != null) { // Duplicates
duplicateRows.add(i);
duplicateRows.add(oldRow);
// oldRow might already be added if third duplicate or same row.
}
}
}
IntStream.rangeOf(0, subsets.length)
.filter(i -> !duplicateRows.contains(i))
.forEach(System.out::println);
return subsets.length - duplicateRows.size();
}
The IntStream part would be in java 7:
for (int i = 0; i < subsets.length; ++i) {
if (!duplicateRows.contains(i)) {
System.out.println(i);
}
}
With java 7 you can safely substitute here putIfAbsent with put.
I have problems with getting certain row or column number from an image data (barcode):
Say I have image[row][col]. How do I access row by a method?
public int getRow (Object[][] image){
return image[row];
}
doesn't work.
Thank you.
Sorry for not figuring out correctly:
There's an image I have set to true and false, for example, image[1][2] = true, image[1][3] = false etc. Now, I need to print each and every image. When I try to do it by
for (row = 0; row < 1 (which is from image[1][]); row++){
for (col = 0; col < 2 (which is from image[][2]; row++){
System.out.print(image[row][col];
}
}
Please let me know if additional info needed.
Assuming your image array contains integer values then you need to cast it to integer and return like this:
return (Integer)image[row][col];
where row,col you need to replace with the desired index.
All Bi-dimensional arrays would work the same way, ObjectType[Row][Column]. This will return the Object at the specified row and column. However, I believe (Could be wrong) that your posted programming statement is casting an Image to an Object array.
Not sure about your actual problem statement. But in the above example you should increase col value instead of row++
It should be
for (col = 0; col < 2 ; col++){
System.out.print(image[row][col]);
instead of
for (col = 0; col < 2 (which is from image[][2]; row++){
System.out.print(image[row][col];
I have a program that is written to work in conjunction with a JTable and a few other Swing elements in order to display and model a backend for a game. The function is supposed to get the first row selected and the last row selected and store them in an array rows at index 0 and 1 respectively. Thanks for your help and hope to understand what is going on here.
public int[] getRows(JTable table) {
rows = new int [2];
rows[0] = table.getSelectedRow();
rowCount = table.getSelectedRowCount() - 1;
rows[1] = rows[0] + rowCount;
return rows;
}
JTable.getSelectedRows() can you help you in this case. Just take the first and last index from the array it returns. Note that it can return an empty array.
I want to replace value of variable array in index [0][2] that the name of coloumn title is "1/y". Previous value is 0.0, I want to replace it with value of calculation result , but when I try to display it, its value is still 0.0, this my code
titleColoumn = new Object[]{"Time (Second)","Medicine", "1/y", "x2", "X/Y", "Y^", "Error"};
//0 1 2 3 4 5 6
allData = new Double[][] {{1.0,1.02,0.0,0.0,0.0,0.0,0.0},
{2.0,0.667,0.0,0.0,0.0,0.0,0.0},
{3.0,0.367,0.0,0.0,0.0,0.0,0.0},
{4.0,0.278,0.0,0.0,0.0,0.0,0.0},
{5.0,0.237,0.0,0.0,0.0,0.0,0.0},
{6.0,0.187,0.0,0.0,0.0,0.0,0.0},
{7.0,0.155,0.0,0.0,0.0,0.0,0.0},
{8.0,0.156,0.0,0.0,0.0,0.0,0.0},
{9.0,0.142,0.0,0.0,0.0,0.0,0.0},
{10.0,0.111,0.0,0.0,0.0,0.0,0.0},
{11.0,0.12,0.0,0.0,0.0,0.0,0.0},
{12.0,0.097,0.0,0.0,0.0,0.0,0.0},
{14.0,0.089,0.0,0.0,0.0,0.0,0.0},
{15.0,0.079,0.0,0.0,0.0,0.0,0.0},
{0.0,0.0,0.0,0.0,0.0,0.0,0.0}};
tableObservation = new DefaultTableModel(allData, titleColoumn);
table.setModel(tableObservation);
int row,coloumn;
//calculation 1/y
row = 0;
coloumn = 1;
int inputRow = 0;
int inputColoumn = 2;
double onePerY;
for(int a=0;a<allData.length;a++){
onePerY = 1/allData[row][coloumn];
//replace value
allData [inputRow][inputColoumn] = onePerY;
inputRow++;
row++;
System.out.println(onePerY);
}
What should I do, to be able to replace it ? all the assistance that you gave, I would appreciate it, thank you
You update array value not TableModel value. Use jTable.getModel().setValueAt() passing inputRow, inputColoumn and appropriate value for them. Your model must be editable. If you use DefaultTableModel it's editable by default.
Read about DefaultTableModel and use it to store the data for your table. When ever there is a change in the data you have to update in the table model.
Also have a look at How to Use Tables
in my Jtable I have a column with boolean values displayed as checkbox.
I can retrive the value cell only when the value is true, when values i false I can't read the values.
I write my code:
int row = jTMezziInt.getRowCount();
int h=0;
while (h<=row){
chk= ((Boolean)jTMezziInt.getValueAt(h, 6)).booleanValue();
//if chk is true I can read;
// if chk is false the execution stopped at the chk assignement;
if (chk)
((DefaultTableModel )this.jTMezziInt.getModel()).removeRow(h);
row = jTMezziInt.getRowCount();
h=h+1;
}
TableColumn Selez = jTMezziInt.getColumnModel().getColumn(6);
}
For define the table I had used netbeas with table editor.
Thanks everybody for help;
There are one of two possible issues here from what I can see. The most likely issue is that you start with h = 0 and then end with h = row (since your while loop iterates while h <= row).
Say for example you have 3 rows in your table. This loop will now run for h = 0, h = 1, h = 2 and h = 3, i.e. it runs 4 times but you only have 3 rows (indexed 0, 1 and 2 there is no row with index 3). This would cause a null pointer exception on its final iteration. Is this the behaviour you are seeing?
To sort it out just make your while loop condition h < row, not h <= row. If this does not work then let me know and we can discuss the other possible issue.
Another issue is that you still increment h even if the row you are checking is deleted. If we are checking row 1 and then delete row 1 then row 2 will become row 1, so we need to recheck this new row 1. So you should only increment h if the current row you are checking is not deleted.
Finally, as an aside, note that if you are using one of the later JDKs there is no need to call booleanValue() (this is called unnecessary unboxing). This will be done automtically for you. So you can change the following:
chk= ((Boolean)jTMezziInt.getValueAt(h, 6)).booleanValue();
to:
chk= (Boolean)jTMezziInt.getValueAt(h, 6);
It's just neater and better style.