Object [][][

I have an object array like

Object[][] data2 ={

{" "," ",

" "," "," "," "},

{" "," ",

" "," "," "," "},

{" "," ",

" "," "," "," "},

{" "," ",

" "," "," "," "},

};

And I want to add

Object[][] data3 ={

{" "," ",

" "," "," "," "},

Is there an a way besides a hard coded for loop?

[1490 byte] By [blackmagea] at [2007-11-27 9:55:28]
# 1

> I have an object array like

Good god, why!!?!?!?

> And I want to add

>

>

> > Object[][] data3 = {

> {" ", " ",

> " ", " ", " "," "},

>

>

> Is there an a way besides a hard coded for loop?

What do you mean "add"? You can't grow an array once it's created.

I have no clue what you're trying to do.

jverda at 2007-7-13 0:25:26 > top of Java-index,Java Essentials,Java Programming...
# 2
Its for creating an empty row in a JTable;
blackmagea at 2007-7-13 0:25:26 > top of Java-index,Java Essentials,Java Programming...
# 3
I still don't know what you mean by "add" though.
jverda at 2007-7-13 0:25:26 > top of Java-index,Java Essentials,Java Programming...
# 4

import java.io.File;

import javax.swing.JDialog;

import javax.swing.JFileChooser;

import javax.swing.JScrollPane;

import javax.swing.JTable;

import javax.swing.table.AbstractTableModel;

import javax.swing.table.TableModel;

import ModelParser.ModelParser;

public class CreateJTable extends JDialog {

//Data members

ReadFiles read;

String returnString=" ";

private boolean DEBUG = false;

public CreateJTable(){

read=new ReadFiles();

String location=runFileChooser();

File file=new File(location);

if(location!=null)

read.runLoad(file);

JTable table = new JTable(new MyTableModel());

for(int i=0; i<read.modelList.size();i++){

String model, brand, category, codeset;

int number;

model=(String) read.modelList.get(i);

brand=(String) read.brandList.get(i);

category=(String) read.categoryList.get(i);

codeset=(String) read.codeList.get(i);

number=i+1;

table.setValueAt(number, i, 0);

table.setValueAt(codeset, i, 4);

table.setValueAt(category, i, 1);

table.setValueAt(brand, i, 2);

table.setValueAt(model, i, 3);

}

JScrollPane scrollpane = new JScrollPane(table);

setSize(600, 600);

getContentPane().add(scrollpane);

setVisible(true);

}//end constructor

//File Chooser the Chooses Directory

private String runFileChooser() {

String location="";

JFileChooser fileChooser=new JFileChooser();

fileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);

int returnVal = fileChooser.showOpenDialog(CreateJTable.this);

if (returnVal == JFileChooser.APPROVE_OPTION) {

location = fileChooser.getSelectedFile().getPath().toString();

}

return location;

}

class MyTableModel extends AbstractTableModel {

private String[] columnNames = {"#",

"Category",

"Brand",

"Model",

"Code Set",

"Location"};

private Object[][] data = {

{" ", " ",

" ", " ", " "," "},

{" ", " ",

" ", " ", " ", " "},

{" ", " ",

" ", " ", " "," "},

{" ", " ",

" ", " ", " ", " "},

};

public int getColumnCount() {

return columnNames.length;

}

public int getRowCount() {

return data.length;

}

public String getColumnName(int col) {

return columnNames[col];

}

public Object getValueAt(int row, int col) {

return data[row][col];

}

/*

* JTable uses this method to determine the default renderer/

* editor for each cell. If we didn't implement this method,

* then the last column would contain text ("true"/"false"),

* rather than a check box.

*/

public Class getColumnClass(int c) {

return getValueAt(0, c).getClass();

}

/*

* Don't need to implement this method unless your table's

* editable.

*/

public boolean isCellEditable(int row, int col) {

//Note that the data/cell address is constant,

//no matter where the cell appears onscreen.

if (col >< 2) {

return false;

} else {

return true;

}

}

/*

* Don't need to implement this method unless your table's

* data can change.

*/

public void setValueAt(Object value, int row, int col) {

if (DEBUG) {

System.out.println("Setting value at " + row + "," + col

+ " to " + value

+ " (an instance of "

+ value.getClass() + ")");

}

//System.out.println(row);

//System.out.println( data.length);

if(row==data.length){

addRow();

}

data[row][col] = value;

fireTableCellUpdated(row, col);

if (DEBUG) {

System.out.println("New value of data:");

printDebugData();

}

}

private void addRow() {

Object[][] data3 = {

{" ", " ",

" ", " ", " "," "},

};

Object[][] data2 = new Object [data.length+1][data.length+1];

for(int i=0; i<data.length; i++){

for(int j=0;j<data.length;j++){

System.out.println(data[i][j]);

}//end second for

}//end for

}

private void printDebugData() {

int numRows = getRowCount();

int numCols = getColumnCount();

for (int i=0; i >< numRows; i++) {

System.out.print("row " + i + ":");

for (int j=0; j < numCols; j++) {

System.out.print(" " + data[i][j]);

}

System.out.println();

}

System.out.println("--");

}

}

}

blackmagea at 2007-7-13 0:25:26 > top of Java-index,Java Essentials,Java Programming...
# 5

Please tell me you didn't intentionally format that array like that.

If you have an array like this:

Object[][] array1 = {

{" ", " ", " ", " ", " ", " "},

{" ", " ", " ", " ", " ", " "},

{" ", " ", " ", " ", " ", " "},

{" ", " ", " ", " ", " ", " "},

};

You can assign another array to a part of the first like this:

Object[][] array2 = { array1[0] };

But those Object references would all be pointing to the same Objects. There wouldn't be any copying going on. If you really wanted to do that, it would be better to declare the 1d array separately, then build the 2d arrays off of those.

Object[] a1 = {" ", " ", " ", " ", " ", " "};

Object[] a2 = {" ", " ", " ", " ", " ", " "};

Object[][] array1 = { a1, a2};

Object[][] array2 = { a1 };

but if you want to have two arrays pointing to two different copies of the same data, then just create it with a loop, especially if it's really going to be filled with the same " " String.

hunter9000a at 2007-7-13 0:25:26 > top of Java-index,Java Essentials,Java Programming...
# 6

Really, this is wat I'm trying to do:

private void addRow() {

Object[][] data3 = {

{" ", " ",

" ", " ", " "," "},

};

Object[][] data2 = new Object [data.length+1][data.length+1];

for(int i=0; i<data2.length; i++){

for(int j=0;j<data2.length;j++){

if(i><data.length){

data2[i][j]=data[i][j];

}

else{

data2[i][j]=data3[0][j];

}

}//end second for

}//end for

data=data2;

}

>

blackmagea at 2007-7-13 0:25:26 > top of Java-index,Java Essentials,Java Programming...
# 7
Use a List<Object[]> instead. A List is a dynamically resizable array. You can add a new Object[] to a List instead of having to copy it into a new larger array.
hunter9000a at 2007-7-13 0:25:26 > top of Java-index,Java Essentials,Java Programming...
# 8
You mean a linkedlist [][]?
blackmagea at 2007-7-13 0:25:26 > top of Java-index,Java Essentials,Java Programming...
# 9
> You mean a linkedlist [][]?He means what he said: List<Object[]>
jverda at 2007-7-13 0:25:26 > top of Java-index,Java Essentials,Java Programming...
# 10

> You mean a linkedlist [][]?

You can use a LinkedList if you want, although an ArrayList is more appropriate for random access. If the number of columns doesn't even change, then you can use an ArrayList<Object[]>. If it will, then use an ArrayList<ArrayList><Object>>.

hunter9000a at 2007-7-13 0:25:26 > top of Java-index,Java Essentials,Java Programming...
# 11
> > You mean a linkedlist [][]?> > He means what he said: List<Object[]>Yes, I did mean List, that's the interface that provides a growable array. The implementation you choose (such as LinkedList, ArrayList, etc.) depends on the situation.
hunter9000a at 2007-7-13 0:25:26 > top of Java-index,Java Essentials,Java Programming...
# 12
Now I can't get the values back. return data[row][col];can be used and there is nodata.get(int 1, int2);
blackmagea at 2007-7-13 0:25:27 > top of Java-index,Java Essentials,Java Programming...
# 13

> Now I can't get the values back.

>

> > return data[row][col];

>

That's because you're still treating data as a 2d array, not a list of arrays.

> can be used and there is no

>

> data.get(int 1, int2);

If data is a List<Object[]>, then use data.get(int1)[int2];

hunter9000a at 2007-7-13 0:25:27 > top of Java-index,Java Essentials,Java Programming...
# 14

> Now I can't get the values back.

>

> [code]

> return data[row][col];

> /code]

>

> can be used and there is no

>

> data.get(int 1, int2);

You know how to get the Nth element from a list, right? If not, see the API docs and if it's still not clear, see the collections tutorial.

Once you've got the Nth element from the list, what type is that? Object[]? List<Object>? Whichever type it is, you now know how to get it's Mth element.

jverda at 2007-7-13 0:25:27 > top of Java-index,Java Essentials,Java Programming...
# 15

How about adding objects?

Object[] a1 = {" ", " ", " ", " ", " ", " "};

private List<Object[][]> data;

public MyTableModel(){

data.add((Object[][]) a1);

}

ClassCast exception

Message was edited by:

blackmage

blackmagea at 2007-7-21 23:12:02 > top of Java-index,Java Essentials,Java Programming...
# 16

a1 is an Object[], not an Object[][].

You can't magically turn an object into something it's not by casting.

You didn't pay attention. List<Object[]>, not List<Object[][]>. Why would you want to add an extra dimension? The List replaces one of your Object[][] dimensions.

jverda at 2007-7-21 23:12:02 > top of Java-index,Java Essentials,Java Programming...
# 17

And a way to instantiate the object?

Object[][] a1 = {{" ", " ", " ", " ", " ", " "}};

private List<Object[][]> data;

public MyTableModel(){

data= new List();

//data.add(a1);

}

blackmagea at 2007-7-21 23:12:02 > top of Java-index,Java Essentials,Java Programming...
# 18

AGAIN: List<Object[]>, not List<Object[][]>

If you're trying to instantiate List, you need this:

http://java.sun.com/docs/books/tutorial/collections/

And if you don't understand the error message, you need this:

http://java.sun.com/docs/books/tutorial/

in particular, this:

http://java.sun.com/docs/books/tutorial/java/IandI/index.html

jverda at 2007-7-21 23:12:02 > top of Java-index,Java Essentials,Java Programming...
# 19

I understand the errors, I'm just not sure how do solve them.

like

Object[][] a2=new Object[row][col];

a2=(Object[][]) value;

data.add(a2);

//data.get(row)[col] = (Object[]) value;

fireTableCellUpdated(row, col);

trying to add a value to the list....2D arrays aren't the easy thing to work with.

blackmagea at 2007-7-21 23:12:02 > top of Java-index,Java Essentials,Java Programming...
# 20
Okay, you seem to be just guessing now. There's nothing I can do until you put in the time to learn the basic concepts.
jverda at 2007-7-21 23:12:02 > top of Java-index,Java Essentials,Java Programming...
# 21

> Its for creating an empty row in a JTable;

import java.awt.*;

import java.awt.event.*;

import javax.swing.*;

import javax.swing.table.*;

class CreateEmptyRowDemo {

private JTable table;

public static void main(String[] args) {

new CreateEmptyRowDemo().go();

}

public void go() {

Object[] columnNames = {"a", "b"};

final DefaultTableModel model = new DefaultTableModel(columnNames, 1);

final JTable table = new JTable(model);

JScrollPane scrollPane = new JScrollPane(table);

JButton addButton = new JButton("Add Row >>");

addButton.addActionListener(new ActionListener() {

public void actionPerformed(ActionEvent e) {

model.addRow(new Object[2]);

}

});

JFrame frame = new JFrame(getClass().getName());

frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

frame.add(scrollPane, BorderLayout.CENTER);

frame.add(addButton, BorderLayout.SOUTH);

frame.pack();

frame.setVisible(true);

}

}

~

yawmarka at 2007-7-21 23:12:02 > top of Java-index,Java Essentials,Java Programming...
# 22

> Object[][] a2=new Object[row][col];

> a2=(Object[][]) value;

For instance, you need to learn about references.

When you say a2=something, you're not copying an object into a2. You're copying a reference.

So in the above, you create a new Object[][], put a reference to it into a2, and then immediately abandon that newly created Object[][] by putting a different reference (that points to a different object) into a2.

> data.add(a2);

What's data and why are you adding an Object[][] to it? Is it List? Why are you still doing List<Object[][]> instead of List<Object[]> after being told at least three times?

> 2D arrays aren't

> the easy thing to work with.

Certainly not if you just try to plow ahead full steam without bothering to pay attention, study, learn, and understand.

Good luck. I don't have patience for this anymore.

jverda at 2007-7-21 23:12:02 > top of Java-index,Java Essentials,Java Programming...
# 23
> Good luck. I don't have patience for this anymore.Cry me a river on your iPhone.~
yawmarka at 2007-7-21 23:12:02 > top of Java-index,Java Essentials,Java Programming...