I want a dynamic matrix, number rows and columns unkonw, filling it by clicking on a button. Bu there is more: I don't want to add entire rows, but just one cell at the time, one click = one cell added. Of course not randomly : 1st cell of 1st row, 2nd cell of 1st row... and then the same of the 2nd row and so one...
I know about UJMP, ArrayList, but it's not quite what I'm looking for. Please be accurate on your answer, thank you in advance.
Use this:
List<List<Integer>> dynamicMatrix = new ArrayList<List<Integer>>();
dynamicMatrix.add(new ArrayList<Integer>());
dynamicMatrix.add(new ArrayList<Integer>());
dynamicMatrix.add(new ArrayList<Integer>());
dynamicMatrix.get(0).add(6);
dynamicMatrix.get(0).add(7);
dynamicMatrix.get(0).add(8);
System.out.println(dynamicMatrix.get(0).get(0)); // 6
System.out.println(dynamicMatrix.get(0).get(1)); // 7
System.out.println(dynamicMatrix.get(0).get(2)); // 8
Related
I have a while condition which is
Row r;
while(row.hasnext())
{
r = row.next();
}
I want after the end of execution the control should go back to the first cell.
You cannot "reset" your row iterator, but you can just get a new one:
Iterator<Row> row = sheet.rowIterator();
Or did I get the question wrong? Then please clarify what type "row" is.
I'm encountering an odd bug while updating the contents of a JTable. I've got a two dimensional array which is storing arrays of values to be displayed by the table (I'm only displaying one row of data at a time). When the user clicks a button, the next set of values (along with the next set of column headers) is displayed, and when the user clicks another button, it displays the previous set. The problem is that the first time I call getModel().setValueAt, on the first array in the two dimensional array, it is removed.
Here's the code that updates the JTable:
for(int i = 0; i < formattedData[displayedDataSet].length - 1; i++) {
TableColumn col = displayTable.getTableHeader().getColumnModel().getColumn(i);
col.setHeaderValue(WeatherData.WEATHER_MAP_KEYS[displayedDataSet * 5 + i]);
displayTable.getTableHeader().resizeAndRepaint();
displayTable.getModel().setValueAt(formattedData[displayedDataSet][i], 0, i);
}
for(String s: formattedData[0]) {
System.out.println(s);
}
I expect the output to be the first array in formattedData, but it is the second. However, commenting out the line with displayTable.getModel().setValueAt causes the correct result to be output. Why is this happening, and how can I update the JTable while preserving my array?
I'm generating a PDF document (for those who have seen his before, yes still) using the iText5 library. Compliments to Bruno Lowagie; I've found it to be a really great API!
However, part of my task is to format some phrases. I used a nested PdfPTable to format the data, but I'm getting bizarre spacing issues. Here is an example of the problem I'm facing:
As you can see, rows with only a couple of phrases in them tend to have huge gaps in between them. I can understand why this is happening; the rows above are stretching the table. Is there a way to only make the table as big as it needs to be, and no larger?
Code
Generating the prices collection
I'm creating the prices with this snippet:
Paragraph pricePara = new Paragraph();
pricePara.add(price);
pricePara.add(generateBreakLine());
pricePara.add(time);
pricePara.add(generateBreakLine());
allPrices.add(pricePara);
where generateBreakLine() returns an empty Paragraph object.
Adding them into the cell
I add them into the cell with the following snippet:
// 5 is just the amount of prices we can fit on one line.
PdfPTable pricesCellValue = new PdfPTable(elements.size() > 5 ? 5 : elements.size());
// Make it appear that the prices are just horizontal, as opposed to in a table.
pricesCellValue.getDefaultCell().setBorder(PdfPCell.NO_BORDER);
// Cycle through each price. Add to cell. Add cell to the table.
for (com.lowagie.text.Element elem : elements) {
PdfPCell cell = new PdfPCell((Paragraph) elem);
cell.setBorder(PdfPCell.NO_BORDER);
cell.setPadding(2);
pricesCellValue.addCell(cell);
}
return pricesCellValue;
The above snippet, I believe, is where I can make sure the table is only as wide as it needs to be, as opposed to filling up all the space around it. How would I go about doing that?
Solved
Found a simple way of fixing it. In stead of changing the amount of columns based on the number of prices, I make sure the column number is always 5, and then append empty cells on:
if(elements.size() < 5)
{
int diff = 5 - elements.size();
for(int x = 0; x < diff; x++)
{
pricesCellValue.addCell(new Paragraph(" "));
}
}
If I've got an list of parameters 'x,y,z' that aren't sorted, is there a straightforward way to write them to particular cells in an excel document created with POI, as though the first two parameters are X and Y coordinates?
For example, I have rows like:
10,4,100
Is it possible to write the value '100' in the cell at the 10th row, 4th column?
Looking at the documentation, it looks straightforward to iterate values into the next row, but I can't see any way of creating a fixed number of rows and columns and writing particular values to only certain cells.
Any advice or suggestions would be appreciated, thanks!
Sure, it's very easy, just remember that POI is 0 based not 1 based in addressing. Assuming you want to write to the 10th row, 4th column, you'd do something like
Row r = sheet.getRow(9); // 10-1
if (r == null) {
// First cell in the row, create
r = sheet.createRow(9);
}
Cell c = r.getCell(3); // 4-1
if (c == null) {
// New cell
c = r.createCell(3, Cell.CELL_TYPE_NUMERIC);
}
c.setCellValue(100);
Disclaimer: This is for a homework assignment.
I am currently working on an assignment where I need to implement an iterable interface in order to pass each array from a square two-dimensional array. This array is supposed to represent a grid of numbers (so I will be referring to them as such [row][col]). My problem is that I want to use the same next method to iterate through the rows and the columns. First, is this possible? Second, any suggestions/hints?
My next method currently looks like this:
public Data[] next(){
Data [] holder = new Data[ray.length];
for (int i = 0; i <ray.length; i++)
holder[i]=ray[counter][i];
counter++;
return holder;}
EDIT: I am aware of being able to switch counter and i in ray[counter][i], but I'm not sure how to have it do both if that's possible.
ray is the multidimensional array and count is an attribute of the Iterator method I've created (It's initialized to 0 and this is the only method that changes it). I know I cannot return the "column" of ray this way, so how would I go about having next call columns and rows?? Thanks for any of the help. I'll be standing by if you have further questions.
My problem is that I want to use the same next method to iterate through the rows and the columns. First, is this possible?
Yes it is possible, assuming you mean what I think you mean. (The phrase "iterate through the rows and the columns" is horribly ambiguous.)
Since this is a homework exercise here are a couple of hints:
You need two counters not one.
When you get to the end of one row you need to go to the start of the next row. (Obviously!) Think about what that means if you've got two counters.
This should be enough to get you on the right track.
I want a row by row iteration and a column by column iteration.
This is also a horribly ambiguous description, but I'm going to interpret it as meaning that sometimes you want to iterate left to right and top to bottom, and other times you want to iterate top to bottom and left to right.
That is also possible:
One possibility is to use an extra state variable to tell the iterator which direction you are iterating; i.e. row within column, or column within row.
Another possibility is to implement two distinct Iterator classes for the two directions.
The problem is that the iterator class is only supposed to have one counter and returns an single-dimension array.
You've (finally) told us unambiguously that the iterator is supposed to return an array. (A good dentist could pull out a tooth quicker than that!)
So here's a hint:
Returning the ith row is easy, but returning the jth column requires you to create a new array to hold the values in that column.
My advice is: transform the 2d array to a list and iterate.
When initialize the Iterator, transform the list. Then you could iterate the list easily.
Following is p-code, you could enrich the implementation in your homework. Hope it helps you!
class TwoDimeIterator implements Iterator<Date> {
List transformedList = new ArrayList();
int cursor = 0;
/** transform to a list row by row.
So you could define your Iterator order.**/
TwoDimeIterator(){
for(int i=0; i < ray.length; i++)
for(int j=0; j < ray[0].length; j++)
transformedList.add(ray[i][j]);
}
public Date next() {
return transformedList.get(cursor++);
}
public boolean hasNext() {
return cursor != transformedList.size();
}
//...
}