I want to add jTable elements to a list. It works fine when jTable have more then one element, but it gives me the the following error when jTable have one element. Why so? How to resolve it. Thank You.
Here is the error.
Error: Exception in thread "AWT-EventQueue-0"
java.lang.ArrayIndexOutOfBoundsException: 1 >= 1
Here is the code:
DefaultTableModel table_tags = (DefaultTableModel)this.jTable_selectedTags.getModel();
int rowCount=table_tags.getRowCount();
Vector data = table_tags.getDataVector();
Vector row = (Vector) data.elementAt(1);
int mColIndex = 0;
List tags_data = new ArrayList(rowCount);
for (int i = 0; i < table_tags.getRowCount(); i++) {
row = (Vector) data.elementAt(i);
tags_data.add(row.get(mColIndex));
}
System.out.println(tags_data);
The issues was in accessing of index. So, after changing
Vector row = (Vector) data.elementAt(1);
to
Vector row = (Vector) data.elementAt(0);
It worked fine.
Your for loop is wrong. You iterate the table using this condition:
for (int i = 0; i < table_tags.getRowCount(); i++)
Problem is that you use the row count to perform the iteration. Given only one element in the Vector your row count would indeed by 1 but using this to iterate over the table would cause it to fail, since the only element at the table would at index 0. Change your above condition to:
for (int i = 0; i < table_tags.getRowCount() - 1; i++)
And I suppose you'll be fine.
Related
I am doing Leetcode 118 Pascal's Triangle and my code is as below. The testcase is five. I should get
[[1],[1,1],[1,2,1],[1,3,3,1],[1,4,6,4,1]].
However, my output is
[[1,4,6,4,1],[1,4,6,4,1],[1,4,6,4,1],[1,4,6,4,1],[1,4,6,4,1]]
I know that the line with comments was not right. But can anyone let me know why I can't just add row to the result? Thanks!
class Solution {
public List<List<Integer>> generate(int numRows) {
List<List<Integer>> result = new ArrayList();
List<Integer> row = new ArrayList();
for(int i = 0; i < numRows; i++ ){
row.add(0,1);
for(int j = 1; j < row.size()-1; j++){
row.set(j, row.get(j) + row.get(j+1));
}
result.add(row); // this line should be result.add(new ArrayList(row));
}
return result;
}
}
Your result list is a list of references to row. The first time you iterate, row refers to an array like [1]. As you keep going through the iterations, you're continuously dereferencing and referencing the variable row.
Since result is essentially [row ,row , row, row ... and so on], you're seeing the result you see here. You'll get the same object as the same block of memory is being referenced and you're overwriting it each time.
On the other hand, new ArrayList(row) actually creates a new block of memory with duplicated data from row - hence this works.
Could anyone help me and point out what is wrong here? All the fields are non-static. Function shuffle returns each time different array.
List<Point[]> someArray = new ArrayList<Point[]>();
for(int i = 0; i < 4; i++) {
Point[] temporary = new Point[50];
temporary = shuffle(pointsArray.getPoints());
someArray.add(temporary);
print(someArray.get(i));
}
The result of print in the first loop is ok since they're different. Right after getting out of the loop, I want to print elements of list someArray again.
for(int i = 0; i < 4; i++) {
print(someArray.get(i));
}
The result here is 4 time of the last element.
Currently the code I have adds a set of attributes into the list below, and returns one giant ArrayList with 70 attributes. I'd like those 70 attributes split into 5 Arrays that represent the 5 columns of the dataset - so I can more easily visualise and then compare the data repetition inside each.
I cannot use an external library like guava since its for an assignment so I can't add any extra files into the project.
I found methods that only work to partition byte lists, and a List<> generic method which I could not make work.
Any suggestions? I know this should be so simple but I cannot find the solution
ArrayList<String> allRows = new ArrayList<String>();
for (int ex = 1; ex < dataExamples; ex++) {
for( int ats = 0; ats < dataAttributes; ats++){
allRows(data[ex][ats]);
}
}
System.out.println(allRows);
This is a simple problem of iterating through the data in the correct manner.
I am unsure if you want Horizontal insertion into the 2d array or vertical insertion. So I've added both, just uncomment the one you want.
// Create fake data
ArrayList<Integer> allRows = new ArrayList<Integer>();
for (int i = 0; i < 70; i++)
allRows.add(i);
System.out.printf("All Data: %s%n", allRows.toString());
// Create 2 Dementional Array
ArrayList<ArrayList<Integer>> twoDRows = new ArrayList<ArrayList<Integer>>();
// Add the 5 rows
for (int i = 0; i < 5; i++)
twoDRows.add(new ArrayList<Integer>());
// Split allRows
for (int i = 0; i<allRows.size(); i++) {
int rowToAddTo;
// Horizontal
rowToAddTo = i/(allRows.size()/twoDRows.size());
// Vertical
rowToAddTo = i%twoDRows.size();
twoDRows.get(rowToAddTo).add(allRows.get(i));
}
// Pretty-ish print
for (ArrayList<Integer> array:twoDRows)
System.out.println(array);
Assuming your array contains your column elements evenly distributed (that is elements 0,5,10,... belong to the same column; 1,6,11,.. to another column, etc.) you can use the mod operation (%) to separate those elements, something like this:
ArrayList<ArrayList<String>> listOfLists = new ArrayList<ArrayList<String>>();
//populate listOfLists
for(int i = 0; i < numberOfColumns; i++){
listOfLists.add(new ArrayList<String>());
}
for(int index = 0; index < totalElements; index++){
//Obtain number of correponding column with mod
int remainder = index % numberOfColumns;
//Insert your element into that corresponding array in listOfLists
listOfLists.get(remainder).add("YOUR ELEMENT");
}
Notice that numberOfColumns is the total number of columns you have, being it 5 or something else.
You may also want to check this answer regarding the sublist() method. Which seems to be also similar to your question.
Cheers
EDIT
Changed the code so it will match any number of columns and add your element to the corresponding array.
I have a List which is having rows fetched from the JDBC.
I am iterating over the List like
for (int i = 0; i < myList.size(); i++) {
myList.get(i);// it returns me Row
// I want to get the column value with column index.
}
Please let me know how can I get all the column values of that row by indexes.
Thanks,
KP
There is a really easy way to access the rows of a 2D array in java
for (int i = 0 ; i < integer2D.length ; i++)
getMyArray(integer2D[i]);
But, I searched in the web to find such easy way to iterate on columns of the 2D-array, like
for (int j = 0 ; j < integer2D[0].length ; j++)
getMyArray(integer2D[][i]);
or
for (int j = 0 ; j < integer2D[0].length ; j++)
getMyArray(integer2D[...][i]);
which works in some programming languages. I just found the class RealMatrix and MatrixUtils that I can convert my array2D to a real matrix and then transpose it and again convert it to an array and iterate on it. But I suppose that there exist a simpler way?
Edit: iterating on rows as I noted in the first piece of code is easy but the main question is how to iterate on columns like the second and third codes work in some other programming languages.
Edit2: As I mentioned in the last paragraph of the main question, the easiest way that I know is transposing the matrix and the iterating on its rows.
If I understand your question, you could use a for-each as an easy way to get each row like
for (int[] row : integer2D) { // <-- for each int[] in the int[][]
for (int val : row) { // <-- for each int in the int[] row
// ...
}
}
For this, you can use the BigMatrixImpl Class of the commons math library. It has a getColumnAsDoubleArray() method which will return the specified column as an array.
Delivering only one index will give you the whole row, so:
integer2D[5] // returns an int[]
will give you an integer array, which is the 6th row in your matrix.
If you supply both indexes directly you get the value of the "cell"
integer2D[5][1] // returns an int
will give you the value of the second column of the 6th row.
This is direct access to your matrix, if you want to iterate through the rows the answer from Elliott is what you are looking for.
Edit: Transposing:
int width = array.length;
int height = array[0].length;
int[][] array_new = new int[height][width];
for (int x = 0; x < width; x++) {
for (int y = 0; y < height; y++) {
array_new[y][x] = array[x][y];
}
}