DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

+ Reply to Thread
Results 1 to 7 of 7

Thread: object [][]

  1. #1
    Join Date
    Jun 2003
    Posts
    17

    object [][]

    hi
    Im quite new to Java and would appreciate some help with this.
    I want to populate an array of type Object from an array of strings.
    Code
    Object object_values=new Object[][];
    String [] data;
    I want to populate object_values with values from data but I cant seem to get the syntax right.
    Does any one have any ideas pls.
    thanks

  2. #2
    Join Date
    Mar 2003
    Posts
    86

    Code

    Seems to be easy. What are you doing? Port you code.

    Sharbov.

  3. #3
    Join Date
    Jun 2003
    Posts
    17
    Im actually trying to create a jtable.Ive decided to use 2 vectors to create a simple table(I know i should be using a model but I want to start from the basics and work my way up :-) )


    So I have an array of strings called observs.
    data_values=new Vector();
    rows=new Vector();
    for (int i=0;i<observs.length;i++){
    data_values.add(observs[i]);

    }

    I then add the data_values vector to the rows vector-
    rows.add(data_values).


    I create a Vector called col_names with the value of "some column name".


    I then create the table
    table=new JTable(rows,col_names).


    The table is being created but is only displaying one row ie the first value from the observs array.
    The table should be displaying at least 10 rows and 1 col.
    what am I doing wrong???
    Thanks

  4. #4
    Join Date
    Jun 2003
    Posts
    17
    hi
    things have moved on now
    I am using vectors to create a table.


    table=new Jtable(data,col_names);
    This works when I want to display the contents of one vector ie data
    But I want to display the contents of another vector as well


    so In want it to lok like this


    vector_1 vector_2
    1 wwf
    2132 wwf
    233 wwf
    2323
    23
    323

    etc etc
    How can I create my tables with these vectors.
    Thanks

  5. #5
    Join Date
    Mar 2003
    Posts
    834
    I'm a little confused about when you're using Vector object and when you're using arrays. I'm also confused as to how you expect to fill a two-dimensional array of Object using a one-dimensional array of Strings.

    However, you're only getting one row because that's the number of times you call rows.add(..). You need TWO loops:
    Code:
    rows = new Vector();
    // for each row...
    for (int i = 0; i < theNumberOfRows; i++) {
      aSingleRow = new Vector();
      // for each cell in the row...
      for (int j = 0; i < observs.length; j++) {
        aSingleRow.add(observs[j]);
      }
      rows.add(aSingleRow).
    }
    ArchAngel.
    O:-)

  6. #6
    Join Date
    Jun 2003
    Posts
    17
    Im trying to create a table with 2 colums and several rows.The tables data will come from 2 arrays.
    I am having problems creating a object [][] data to create a table using a table model.
    Object [][]data=new Object [][]data(that somehow contains the values of the 2 other arrays)
    ie jtable table=jtable(data,colnames_array)
    How can I feed 2 separate arrays into a multidimensional object array?
    thanks

  7. #7
    Join Date
    Mar 2003
    Posts
    834
    Didn't the example above help? OK. How to copy two one-dimensional arrays into a single two-dimensional array:
    Code:
    public class TwoBecomeOne {
            public static void main(String[] args) {
                    String[] s1 = {"A1", "A2", "A3"};
                    String[] s2 = {"B1", "B2", "B3"};
                    Object[][] o = {s1, s2};
    
                    displayArray(o);
            }
    
            public static void displayArray(Object[][] array) {
                    for (int i = 0; i < array.length; i++) {
                            for (int j = 0; j < array[i].length; j++) {
                                    System.out.print("\t" + array[i][j]);
                            }
                            System.out.println();
                    }
            }
    }
    This works because the 2D Object array may hold two references to two 1D String arrays.
    ArchAngel.
    O:-)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
HTML5 Development Center
 
 
FAQ
Latest Articles
Java
.NET
XML
Database
Enterprise
Questions? Contact us.
C++
Web Development
Wireless
Latest Tips
Open Source


Top DevX Stories

Easy Web Services with SQL Server 2005 HTTP Endpoints
JavaOne 2005: Java Platform Roadmap Focuses on Ease of Development, Sun Focuses on the "Free" in F.O.S.S.
Wed Yourself to UML with the Power of Associations
Microsoft to Add AJAX Capabilities to ASP.NET
IBM's Cloudscape Versus MySQL


Sponsored Links