Creating a new JTable from current JTable view - view

I am working on a project that involves JTable and performing sorting and filtering operations on it. I am done with the sorting and filtering part and now I want to be able to create a new table from the current view of older table.
e.g. If I apply certain filters to my old table, some rows are filtered out. I don't want those filtered out rows in my new table. I figured that I can convert the new row indices to model indices and add the cell values manually to new table's model, but I was wondering if there's any other efficient way to do this?
Following is what I ended up doing:
//this code block will print out the rows in current view
int newRowCount = table.getRowCount();
int newColumnCount = table.getColumnCount();
for (int i = 0; i < newRowCount; i++) {
for (int j = 0; j < newColumnCount; j++) {
int viewIndex = table.convertRowIndexToModel(i);
String value = (String) model.getValueAt(viewIndex, j);
System.out.print(value + "\t");
}
System.out.println();
}

no need for any index conversion, simply ask query the table instead of the underlying model
for (int i = 0; i < table.getRowCount(); i++) {
for (int j = 0; j < table.getColumnCount(); j++) {
Object value = table.getValueAt(i, j);
System.out.print(value + "\t");
}
}
Note: better rename the i/j to row/column for readability, was too lazy ;-)

Related

processing animation in loop

so its my first day using processing and i need a bit of help to start with this is my code, im doing the selection sort:
int[] numbers; // Declare array
int currentmin;
int exchange=0;
void setup() {
frameRate(0.1);
size(500,500);
numbers = new int[10]; // Create array with 10 cells
background(105);
for (int i = 0; i < numbers.length; i++) { // random numbers from 1 to 100
int r = int(random(1,100)); // create random numbers
for (int j=0; j<numbers.length; j++) { //make sure not duplications
if(numbers[j]==r)
{r=r+1;}
}
numbers[i]=r; //fill array with random numbers
println(r);
}
fill(255,0,0);
for (int i = 0; i < numbers.length; i++) { //draw rectangles
rect(i*40+10,400 ,35, -numbers[i]);
}
for (int j=0; j < numbers.length; j++){ //set pivot number
currentmin=numbers[j];
for (int i=j+1; i < numbers.length; i++){ //find lowest number in array
if (numbers[i] < currentmin) {
currentmin = numbers[i];
}
}
for ( int i=j; i<numbers.length;i++){ //swap pivot with lowest number
if ( numbers[i]==currentmin){
exchange=numbers[j];
numbers[j]=numbers[i];
numbers[i]=exchange;
//here
}
}
}
}
void draw() {
background(105);
for (int z = 0; z < numbers.length; z++) {
rect(z*40+10,400 ,35, -numbers[z]);
}
}
as you can see where i have the comment here im trying to make the thing animated and to see the rectangles change place on each iteration in the first loop, but its not working... any help ? i tried calling draw(); hopelessly but it didnt work... anyway i can get a help ?
This isn't exactly a trivial animation to execute. You should really start with something simpler.
But to answer your question, you wouldn't put your animation inside a for loop. Instead, you need to make it so the draw() function draws a single frame of the animation. Processing automatically calls the draw() function 60 times per second, so by changing what's drawn each time, you create an animation.
Shameless self-promotion: I wrote a tutorial on creating animations in Processing available here.

Permuting Entries in main Diagonal

If i have 8*8 matrix. Having the following values
Want it to permute like following
Such that diagonal is divided into two part Top and Bottom. First entry 1 is filled at first location of Bottom part. Second entry 2 is placed at first location of top part and upto so on.
I want to extend this idea to other diagonals of matrix.
for(int row = 0; row < dimension; row++){
for(int col = 0; col < dimension; col++){
if(row == col){
// Do something to this cell.
}
}
}
I think that the most straight-forward solution is to first save the diagonal values into additional array and then copy its elements in cycle into their position.
Possible solution in C:
int top = 0; // Index where the top part currently is
int bottom = SIZE/2; // Index where the bottom part currently is
int diagonal[SIZE]; // Additional array, which stores the diagonal elements
for(int i = 0; i < SIZE; i++)
diagonal[i] = matrix[i][i]; // Fill the additional array with diagonal elements
for(int i = 0; i < SIZE; i++)
{
if(i%2 == 0) // put it to lower part
{
matrix[bottom][bottom] = diagonal[i];
bottom++; // Update the bottom index
}
else // put it to upper part
{
matrix[top][top] = diagonal[i];
top++; // Update the top index
}
}
Note, that SIZE has to be even number, in order to make it work like you described.

Increment instance name

Is there a way to effectively increment a instance name in, for example, a for-loop, like this?
for (int i=0; i<10; i++) {
City city+i = new City();
}
That code obviously does not work, but what can I do to create 10 objects, named City0, City1, City2 etc?
You can add the cities to a list or an array and access them by index. Example of list approach in Java:
List<City> cities = new ArrayList<>();
for (int i = 0; i < 10; i++) {
cities.add(new City());
}
Then, you access it like city.get(0) for first one city.get(1) for second etc.
City city[10]; // declare an array of "City" objects, 10 in size
for(int i = 0; i < 10; i++){
city[i] = new City(); //changed to an array
}

How to set a number string into a real string formation?

I'm now using NPOI to cope with Excel export, and here's my codes (part in .NET):
int rowIndex = 1;
for (int i = 0; i < dt.Rows.Count; i++)
{
IRow dataRow = sheet.CreateRow(rowIndex);
for (int j = 0; j < cellCount; j++)
{
cell = dataRow.CreateCell(j,CellType.String);
cell.SetCellValue(new HSSFRichTextString(dt.Rows[i][j].ToString()));
}
rowIndex++;
}
What makes me feel surprised is there's a list whose number string is "20150525", and it will be analyzed as "2015……E+10" formation (scientific number formation). However I wanna keep it as a string value. So How?
Thanks!
In fact we have to set a CellStyle, snippet of sample codes is below:
IRow row = book[0].CreateRow(rowIndex + 1);
ICell rowCell = null;
rowCell = row.CreateCell(colIndex);
rowCell.SetCellValue(realCellValue);
ICellStyle cellStyle = book.CreateCellStyle();
cellStyle.DataFormat = HSSFDataFormat.GetBuiltinFormat("#");
rowCell.CellStyle = cellStyle;

Nested loop complexity

I have several lists of varying size, each index of the list contains both a key and an object : list1.add('Key', obj).
The lists are all sorted.
My aim is to iterate through the list and match 1 or more items in list 2,3,4 or n to an item in the mainList using the key.
Currently I do something along the lines of:
for i to list1 size
for j to list2 size
if list1[i] = list2[j]
do stuff
As I loop through I'm, using boolean values to exit quickly using a if current != previous check and I'm deleting the object from the list I take it from.
it's working fine but I now have another list that I need to match and possible another n lists afterwards. The lists are of different sizes.
the 2 options that I can see are to either repeat the above segment of code several times where the inner list is changed - I do no like this approach.
The other option is to extend the above and once one inner loop is finished, move onto the next:
for i to list1 size
for j to list2 size
if list1[i] = list2[j]
do stuff
for k to list2 size
if list1[i] = list2[k]
do stuff
I'd like to think I'm correct in thinking that the 2nd is more efficient however I'm unsure. Also, is there a better way?
Thanks for any advice / help.
If the lists are all sorted then you only need to iterate though each list once; on each iteration of the main list, iterate through a secondary list (starting at the previously saved index, initialized to 0) until you find an index whose value is greater than the current value of the main list, save this index, and proceed to the next secondary list.
Array<Integer> indices = new Array(n-1); // indices for every list except list1
for(int i = 0; i < indices.size; i++) {
indices[i] = 0;
}
for(int i = 0; i < list1.size; i++) {
Value curVal = list1[i];
while(indices[0] < list2.size && list2[indices[0]] <= curVal) {
if(list2[indices[0]] == curVal) {
// do stuff on list2[indices[0]]
}
indices[0]++;
}
while(indices[1] < list3.size && list3[indices[1]] < curVal) {
if(list3[indices[1]] == curVal) {
// do stuff on list3[indices[1]]
}
indices[1]++;
}
// etc
}
You can avoid the copy-pasting by using something like a ListIterator that contains a list and its current index; then on each iteration of the main loop you'll iterate through a list of ListIterators in lieu of the copy-pasted code block
public class ListIterator {
int index = 0;
List<Value> list;
Value curVal() {
return list[index];
}
boolean hasNext() {
return (index < list.size);
}
}
List<ListIterator> iterators;
for(int i = 0; i < list1.size; i++) {
Value curVal = list1[i];
for(int j = 0; j < iterators.size; j++) {
ListIterator iterator = iterators[j];
while(iterator.hasNext() && iterator.curVal() <= curVal) {
if(iterator.curVal() == curVal) {
// do something with iterator.curVal()
}
iterator.index++;
}
}
}
This is time complexity O(n) where n is the sum of the lengths of all of your lists
Edit: If it's difficult to compare keys via <=, then you can use a Set implementation instead. Add the List1 keys to a Set, then iterate through the remaining lists testing for set membership.
Set<String> set = new Set(List1);
Array<List> lists = new Array();
// add lists to Array<List>
for(int i = 0; i < lists.size; i++) {
List currentList = lists[i];
for(int j = 0; j < currentList.size; j++) {
if(set.contains(currentList[j]) {
// do something
}
}
}

Resources