Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / October 2005

Tip: Looking for answers? Try searching our database.

array size increase

Thread view: 
Jacques Chaurette - 14 Oct 2005 23:09 GMT
Hi all, this might be a dumb question but once an array is set say
data[10][3] is it possible to add another row to the array by saying
something like:

for (i=0; i<3; i++)  {
         data[data.length+1][i] = row[i];
         }

where row[i] represents the row to add.

or do I have to create a new array of a different size?

Cheers,

Jacques
hilz - 14 Oct 2005 23:23 GMT
> Hi all, this might be a dumb question but once an array is set say
> data[10][3] is it possible to add another row to the array by saying
[quoted text clipped - 11 lines]
>
> Jacques

Don't think you can.
Why not use ArrayList of ArrayLists ?

Maybe someone might have a better idea...but that is what just came to
my mind!
Jacques Chaurette - 14 Oct 2005 23:55 GMT
Hi Hilz, my problem is related to jtable where I need to add a row using an
extension of an abstacttablemodel. My jtable uses an object array and I am
thinking that my problem is that I cannot increase the tablemodel array
size. It seems that jtable only accepts object arrays or vectors as
containers for the grid data and not array lists.

Do you have any suggestions?

Jacques

>> Hi all, this might be a dumb question but once an array is set say
>> data[10][3] is it possible to add another row to the array by saying
[quoted text clipped - 17 lines]
> Maybe someone might have a better idea...but that is what just came to my
> mind!
Rhino - 15 Oct 2005 00:09 GMT
> Hi Hilz, my problem is related to jtable where I need to add a row using an
> extension of an abstacttablemodel. My jtable uses an object array and I am
[quoted text clipped - 3 lines]
>
> Do you have any suggestions?

Yes, use Vectors or Collections, either of which can grow as needed after
they have been created.

Rhino
Jon Martin Solaas - 15 Oct 2005 00:16 GMT
> Do you have any suggestions?

System.arrayCopy could be of help to you. You can't shrink or grow an
array, but you can easily copy contents from one array to a larger one.

Signature

jon martin solaas

Jacques Chaurette - 15 Oct 2005 00:24 GMT
So if I use a vector instead of an array since a vector has only 1 dimension
I will simulate the array construction in the vector and access row data by
increments of 3 elements for an array that has 3 elements per row, would you
agree?

Jacques
.

>> Do you have any suggestions?
>
> System.arrayCopy could be of help to you. You can't shrink or grow an
> array, but you can easily copy contents from one array to a larger one.
Luke Webber - 15 Oct 2005 01:38 GMT
> So if I use a vector instead of an array since a vector has only 1 dimension
> I will simulate the array construction in the vector and access row data by
> increments of 3 elements for an array that has 3 elements per row, would you
> agree?

Um, no. Your vector should be a Vector of Vectors, such that you can
reference (say) Row 0, Column 3 like this (presuming it's a String here)...

  String val = (String) ((Vector) vec.elementAt(0).elementAt(3)));

...or something like that. Check the text on the JTable(Vector, Vector)
constructor.

Luke
Jacques Chaurette - 15 Oct 2005 02:12 GMT
Excellent.

>> So if I use a vector instead of an array since a vector has only 1
>> dimension I will simulate the array construction in the vector and access
[quoted text clipped - 11 lines]
>
> Luke
Roedy Green - 15 Oct 2005 02:23 GMT
>So if I use a vector instead of an array since a vector has only 1 dimension

You would not use Vector since it is only needed for multithread.  You
would use ArrayList.  You would use an Arraylist<Object[]> where each
element of the Arraylist in a simple array -- a row.

Your rows don't grow and shrink.  If they did, you could have an
ArrayList of ArrayLists.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

hilz - 15 Oct 2005 07:39 GMT
> You would not use Vector since it is only needed for multithread.  You
> would use ArrayList.  You would use an Arraylist<Object[]> where each
> element of the Arraylist in a simple array -- a row.

I like that!
That is probably more appropriate for the TableModel case than the
ArrayList of ArrayLists.
Jacques Chaurette - 15 Oct 2005 13:02 GMT
Can someone give an example of how to initialize with dummy values an
araaylist of arraylist, say simulating 2 rows with 3 columns.

Thanks.

>> You would not use Vector since it is only needed for multithread.  You
>> would use ArrayList.  You would use an Arraylist<Object[]> where each
[quoted text clipped - 3 lines]
> That is probably more appropriate for the TableModel case than the
> ArrayList of ArrayLists.
Jacques Chaurette - 15 Oct 2005 13:22 GMT
I think I can generate a 1 dim. arraylist doing the following:

String[] values= {"val00", "val01","val02"};

ArrayList  data = new  ArrayList();
for (int i = 0 ; x < values.length ; i++)
  list.add(values[x]);
for (int i = 0 ; i < data.size() ; i++)

For my jtable application I need an arraylist that can give me access to
individual access to columns and rows, how can I generate an arraylist of
arraylist? and how will I access the individual elements. Thanks greatly for
any help, this sounds simple I am sure for all of you but I cannot find
examples of this anywhere on the net.

Jacques

> Can someone give an example of how to initialize with dummy values an
> araaylist of arraylist, say simulating 2 rows with 3 columns.
[quoted text clipped - 8 lines]
>> That is probably more appropriate for the TableModel case than the
>> ArrayList of ArrayLists.
hilz - 15 Oct 2005 18:18 GMT
> I think I can generate a 1 dim. arraylist doing the following:
>
[quoted text clipped - 25 lines]
>>>That is probably more appropriate for the TableModel case than the
>>>ArrayList of ArrayLists.

Roedy's idea of ArrayList of simple array of objects is better suited.
so it could be something similar to this:

ArrayList  data = new  ArrayList();

String[] row0= {"val00", "val01","val02"};
String[] row1= {"val10", "val11","val12"};

data.add(row0);
data.add(row1);

//to access the last value in row0 do this:
String aValue = ((String[])data.get(0))[2];

//to get the first value in row1 do this:
String aValue = ((String[])data.get(1))[0];

HTH
Jacques Chaurette - 15 Oct 2005 20:29 GMT
Hilz, this doesn't seem to work. I have defined row0 as  string array

I have these instructions where I create the tablemodel

ArrayList  data = new  ArrayList();

       data.add(row0);

and the compiler creates this error on the same line data.add(..);

"pipe_size.java": <identifier> expected at line 571, column 17

I have tried changing the type of  row0 to Object but this does not help.

Jacques

>> I think I can generate a 1 dim. arraylist doing the following:
>>
[quoted text clipped - 44 lines]
>
> HTH
Jacques Chaurette - 15 Oct 2005 22:14 GMT
Right, let me re-phrase this. Since my purpose is to add a row to my Table I
need to create an addRow method such as:

************************************************
class MyTableModel extends AbstractTableModel {

   String[] columnNames = {"Nom. dia.","Dia. (in)",
                                       "Instal. Cost",
                                       };

             ArrayList  data = new  ArrayList();

       public void addRow(Object row) {
         data.add(row);
         fireTableDataChanged();
       }
       public int getColumnCount() {
           return columnNames.length;
       }

       public int getRowCount() {
           return data.size()/3;
       }

       public String getColumnName(int col) {
           return columnNames[col];
       }

       public Object getValueAt(int row, int col) {
                  return (String[])data.get(row))[col];
       }

               public Class getColumnClass(int c) {
           return getValueAt(0, c).getClass();
       }

           public boolean isCellEditable(int row, int col) {
                          return true;
                }

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

           data[row][col] = (String[])data.set(row, Value))[col];
                     fireTableCellUpdated(row, col);

           }
       }

           }
********************************************

The getValue and setValue follow your recommendations but they do not work,
compiler halts at the getValue method and says "expecting ";"......"

Jacques

> Hilz, this doesn't seem to work. I have defined row0 as  string array
>
[quoted text clipped - 60 lines]
>>
>> HTH
Roedy Green - 16 Oct 2005 05:19 GMT
>     data.add(row);
>          fireTableDataChanged();
>  

That's fine as a first cut, but you are telling Swing that the ENTIRE
table changed and so it had better repaint the WHOLE thing.

There is another fire* method to tell it you added just one row. Then
Swing knows all it has to do is scroll and paint one new row, much
faster.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 16 Oct 2005 05:23 GMT
>   data[row][col] = (String[])data.set(row, Value))[col];
>                      fireTableCellUpdated(row, col);

This code presumes your data is held in an Object[][]

You are now using a ArrayList<Object[]>

What you need to do is fetch the row from the ArrayList. Then plop the
cell data into the column of that row. There is no need to write
anything back to the ArrayList. Its pointer to the row is still the
same.

I suggest peppering your code with System.out.printlns so you can
follow what is happening or else use an IDE with a source level
debugger so you can watch what is happening.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

HalcyonWild - 17 Oct 2005 11:37 GMT
> You are now using a ArrayList<Object[]>
>
> What you need to do is fetch the row from the ArrayList. Then plop the
> cell data into the column of that row. There is no need to write
> anything back to the ArrayList. Its pointer to the row is still the
> same.

You cannot use the JTable constructor then, it accepts only Vector from
Collections, nothing else. I checked with the API, both 1.4.2 and 1.5.0

One time use of Vector isnt a bad idea after all, when there is no
other simple option available.
Roedy Green - 18 Oct 2005 05:22 GMT
>You cannot use the JTable constructor then, it accepts only Vector from
>Collections, nothing else. I checked with the API, both 1.4.2 and 1.5.0

I have never done it that way. I have always created a TableModel
separately. Then the internal structure is up to you.

Vector and ArrayList work quite similarly. Vector just gives you more
thread safety.

Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Roedy Green - 15 Oct 2005 22:42 GMT
>"pipe_size.java": <identifier> expected at line 571, column 17

You need context to analyse compiler messages.  The error is often in
a declaration or some nearby code.
Signature

Canadian Mind Products, Roedy Green.
http://mindprod.com Again taking new Java programming contracts.

Hendrik Maryns - 17 Oct 2005 10:56 GMT
hilz schreef:
> ArrayList  data = new  ArrayList();
>
[quoted text clipped - 9 lines]
> //to get the first value in row1 do this:
> String aValue = ((String[])data.get(1))[0];

I'd suggest to use generics, so that you get rid of a lot of parentheses:

ArrayList<String[]>  data = new  ArrayList<String[]>();

String[] row0= {"val00", "val01","val02"};
String[] row1= {"val10", "val11","val12"};
// and here I would use an enum, see
http://java.sun.com/j2se/1.5.0/docs/guide/language/enums.html

data.add(row0);
data.add(row1);

//to access the last value in row0 do this:
String aValue = data.get(0)[2];

//to get the first value in row1 do this:
String aValue = data.get(1)[0];

H.
Signature

Hendrik Maryns

==================
www.lieverleven.be
http://aouw.org



Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.