2-dimension array

hi,JDC people.
i want to use a 2-dimension array, the length of the first dimension is know, the length of the second dimension varies. how could i define and add elements to the array? shall i use ArrayList?
can anyone give some hint or simple example? thanks.

In Java, you don't (strictly speaking) have a two-dimensional array, you have an array of arrays.
If you know the length of the first array (dimension), you can declare it thusly:Object[][] objectsArray = new Object[2][]; // You've declared and initialised one 'dimension';You can then declare each of the second arrays (dimensions) in turn:objectsArray[0] = new Object[5];
objectsArray[1] = new Object[936];
// etc.You then refer to each Object in the array(s), like this:Object something = objectsArray[0][3]; // Fourth object in first array
Object somethingElse = objectsArray[1][935]; // 936th object in second arrayHope this is helpful...
Chris.

Similar Messages

  • How can i declare 2 or more dimension array as function parameter???

    I am new to Objective-C and Cocoa. and now I have a problem.
    I want to send 2 dimension array to function but it cannot compile the error is "array type has incomplete element type" Please help me
    This is my Interface
    + (void) reValueInArray: (int[][] ) labelValue replaceValue: (int) oldValue withValue: (int) newValue;
    and one more question
    How can i get length of array
    1. array that declare as array at first (int a[5] for example)
    2. array that declare as pointer at first (int a* for example)
    Thank you Very much

    Satake wrote:
    a 2 dimension array is just an array of arrays correct?
    No. It isn't.
    so it'd be something like
    - (void)arrayManipulator:(NSArray *)aTwoDimensionArray;
    You're mixing metaphors. The original poster isn't using NSArray, but, rather, C arrays.
    + (void) reValueInArray: (int[][] ) labelValue replaceValue: (int) oldValue withValue: (int) newValue;
    In C, a multi-dimensional array is just one big, single-dimensioned array that uses fixed sizes for all but one of the component arrays to find a particular element. That means that you can't ever do something like:
    There are several workarounds.
    1) Use NSArray, as suggested in the first reply. That will solve this problem and lots more. With NSArray, you really an have arrays inside of arrays.
    2) Treat the array as just a plain old pointer in the function signature, then cast it back to the properly dimensioned array.
    3) Create a true array of arrays in C, using dynamic allocation. More trouble than it's worth.
    4...N) Other options that are probably more trouble than they're worth.

  • Is it possible to make a 2D array (or whatever-dimension) array like..

    Is it possible to make a 2D array (or whatever-dimension) array like this...
            collumn 1                   collumn 2
    Emulated Address   Real Memory address*
               (int)                            (int *)
    +----------------------+----------------------------+
    |            0                |              0xA0               | <-- row 1
    |            1                |              0xA1               | <-- row 2
    |            2                |              0xA2               | <-- row 3
    +----------------------+----------------------------+
    * A = Address.
    is it possible to make an array like that?
    if it is, please tell me how to do it...
    thanks.
    ... I'm trying to make an emulator to emulate the simplest memory.

    Given your other posts, I'm assuming you mean in C, right?
    If so, the answer is yes, but specifically how will depend on a needed clarification of your question.  What you present doesn't really need to be a 2 dimensional array, just one: that looks like a simple list of memory addresses, right?
    At the simplest you can declare an array with two dimensions `iny myarray[2][3];` but to make the table you put up there you'd only need `int *myarray[3];`
    If you also wanted space allocated somewhere that each element of that list pointed to, you could allocate them separately:
    int *myarray[3];
    myarray[0] = (int *) malloc(sizeof(int));
    myarray[1] = (int *) malloc(sizeof(int));
    myarray[2] = (int *) malloc(sizeof(int));
    Obviously with many entries this should be in a loop.  Perhaps not as obviously, why would you not just malloc a larger block of memory to start with?
    What is the end goal?
    EDIT: actually, upon rereading your question, the mallocs are probably irrelevant.  `int *myarray[3]` will get you the array you are looking for.  Just realize that until you point those pointers to memory you 'own' you shouldn't assign to or read from them.
    Last edited by Trilby (2013-04-19 10:06:31)

  • Two dimension array?

    Show me the code for two dimension array in JSP!
    Thanks in advance!

    Hi,
    I am new in Java but maybe I can help you:
    Two-dimnsional arrays you can declare in following way:
    String[][] someArray;
    When you want to allocate space for new array you can do something like this:
    someArray = new String[5][5];
    All Arrays have a data variable called length, which you can inspect to find out how many elements there are in the array.
    For multi-dimensional arrays, each dimension has a length property.
    int[] [] a1 = {{ 0, 1, 2 },{ 3, 4, 5 }};
    for (int i=0; i < a1.length; i++) {
    for (int j=0; j < a1.length; j++) {
    prt(i + "," + j + "=" + a1[i][j]);
    Hope this is of help to you.
    Mirkos

  • Selecting one dimension of a two dimension array?

    hi, i want to select only one dimension of an array and put it into an arraylist (or casting the array verbatim into the arraylist)
    for example, here is an two dimension array:
    Object[][] data =
         {  { new Date(), "A", new Integer(1), Boolean.TRUE },
         { new Date(), "B", new Integer(2), Boolean.FALSE },
         { new Date(), "C", new Integer(9), Boolean.TRUE },
         { new Date(), "D", new Integer(4), Boolean.FALSE}
         };and i want to cast it into an arraylist, what i have in mind (a bit troublesome is the following:)
    ArrayList dataArray = new ArrayList();
    dataArray.add(data[0]);
    dataArray.add(data[1]);
    dataArray.add(data[2]);
    dataArray.add(data[3]);
    System.out.println(dataArray);when come upon the last System.out.println, i got some seemly useless addresses:
    [[Ljava.lang.Object;@f73c1, [Ljava.lang.Object;@789144, [Ljava.lang.Object;@1893efe, [Ljava.lang.Object;@186c6b2]
    how do you accomplish casting a two dimension array into an arraylist, but the second dimension of the arraylist is kept as Array of Objects? thanks alot!

    First, the strings that are printed out are the value of the hashcode for the specific object. That is the default value that is printed out for any object reference in a class that inherits the toString() method from the Object class. This is true for arrays. Your ArrayList contains array references in it..
    So, you are printing the object hashcodes for the arrays in your ArrayList, instead of the contents of those arrays. It would be better if you created ArrayLists, or even designed an object that stores your data, and stored those inside your ArrayList. ArrayList has an overwritten toString() method that will print the contents instead of the object hashcode, so an ArrayList containing ArrayLists would print your data the way you want. If you define your own class, you will need to provide a toString() method that prints the contents the way you want.
    PS, look at http://java.sun.com/j2se/1.5.0/docs/api/java/util/AbstractCollection.html#toString() for more information on the toString() method used by ArrayList.
    � {�                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

  • Matrix in  java using  two dimension array

    hi!
    Everybody here's a problem I have cpould any body out there give nice piece of sourcr code for "matrix in java using two dimension array"I have to give a presentation how it works.Yes I'd appreciate as many example as possible
    If anybody can??
    Please help
    regards
    ardent

    nice piece of sourcr
    code for "matrix in java using two dimension
    n arraySure,
    int[][] matrix = new int[5][5];
    That declares a two-dimensional 5 by 5 array with integer elements. All elements are initially 0.
    For further information check you favourite Java textbook.

  • Initialize 2 dimensions arrays

    Hi everybody.
    Im using Oracle 11g, on Linux Centos.
    My problem is with 2 dimensions array.
    I have defined a type in the database in this way :
    create or replace
    TYPE ARRAY_N_12 is varray(12) of number;
    Now I try to do the next :
    create or replace
    procedure PRUEBA_INDEX as
      type xyzp is table of array_n_12 index by varchar2(5);
        XX xyzp ;
      I number;
      Z number;
    begin
       for I in -1 .. 100
       LOOP
       for Z in 1 .. 12
       LOOP
         XX(I)(Z) := i;
       end LOOP;
       end LOOP;
      and follow other commands.
    When I compile this it do it without errors. But when I try to run it it say me error :
    ORA-06531 , not initialized recompilation.
    I understand that I need initialized the array. But I dont find the way.
    If I do the next :
    xx xyzp := xyzp(); It say me no valid in this context.
    so I have been trying another ways but I dont find.
    Can anybody help me with this?.
    Thanks in advanced & regards.

    Mixing VARRAY and INDEX BY collection types doesn't make this any easier to follow.
    FOR i IN -1 .. 100 LOOP
       -- initialise element
       xx(i) := array_n_12();
       FOR z IN 1 .. 12 LOOP
          -- extend element
          xx(i).extend;
          -- assign element
          xx(i)(z) := i;
       END LOOP;
    END LOOP;

  • 2 dimension array size error

    Hello,
    In attached file a exemple of my problem. I delete a 2 dimension array and display the size. I can notice in "size 2" (see the exemple") that the size is not at 0. Is It a normal behaviour?
    Thanks in advance, Daniel.
    Solved!
    Go to Solution.
    Attachments:
    ArraySize.vi ‏17 KB

    > So, there is no link between the display and the size array function
    I guess you are right - the display shows values - not memory allocation.
    One could make the case that it would be nice if the Delete from Array function was smart enough to realize that you just deleted the last of the array so why not get rid of any remaining rows/columns. On the other side of the coin, the Delete from Array function is not intended to be a Delete Array function and there may be performance issues on large arrays to add that functionality.
    To be honest, I was a bit puzzled by this until I saw the help documentation (i.e. rows or columns - not both). That's when I decided to check the array to see if it really was the size that was reported - and it was.
    You can also see this same functionality if you make an 2D array constant on the block diagram. Set values for the first column. Then right click and pick delete column. The array appears empty. Now enter a value in row zero, column zero. You will see that values for all of the rows you had previously set will be created. The memory for the rows was retained. 
     - very interesting find Daniel.
    steve
    Help the forum when you get help. Click the "Solution?" icon on the reply that answers your
    question. Give "Kudos" to replies that help.

  • How to print out the position of 2 dimension arrays?

    there are 2 dimension arrays, how can print out the position of value than 4?
    for example
    0 2 3 2 2 2
    0 1 1 2 5 3
    1 2 3 3 2 1
    print out:
    The pos is ( 5,2).

    Hello
    If I understand your question correctly, you are trying to print out the indexes where the value in your matrix is greater than 4
    If this is the case, then what you need to use is nested for loops e.g.
    for (int i = 0; i< array[0].length; i++)
    for (int j = 0; j< array[].length; j++)
    int value = array[i][j];
    if (value > 4)
    System.out.println("The pos is (" + i + "," + j + ")" );
    Regarding the syntax I have used to get the size of the array, I am not sure if this is correct, but you should have an idea of what I am doing.
    Sajid

  • Can I write a subVI that accepts a generic-dimension array?

    I'd like to write a subVI with an input that is an array. The thing is, sometimes the array will be 1D, sometimes 2D, sometimes more.
    If I try to pass an array reference, the reference "knows" what dimension the array is.
    If I pass the array directly then I'm hardwired to accept whatever kind of array I put in my subVI as the input.
    Is there a way to pass an array reference that is NOT specifically dimensioned?
    A related question: Is there a way to get the array's dimension from a property node? As far as I can tell the only way is the get the value property and then use Array Size. It seems like the array's dimension should be a property.

    Hello again Bmarsh
    > I'd like to write a subVI with an input that is an array. The thing
    > is, sometimes the array will be 1D, sometimes 2D, sometimes more.
    >
    > If I try to pass an array reference, the reference "knows" what
    > dimension the array is.
    >
    > If I pass the array directly then I'm hardwired to accept whatever
    > kind of array I put in my subVI as the input.
    >
    > Is there a way to pass an array reference that is NOT specifically
    > dimensioned?
    When you create the array control refnum on the subVI, make sure it is not
    strictly typed to an actual array. Create it from the palette (Control
    Refnum)
    and select Array class or otherwise unselect "Include Data Type" in the
    right-click menu of the control refnum. Then the reference is generic to an
    N dimensi
    onal array.
    >
    > A related question: Is there a way to get the array's dimension from a
    > property node? As far as I can tell the only way is the get the value
    > property and then use Array Size. It seems like the array's dimension
    > should be a property.
    You can have it indirectly reading the "Index Values" property, which is an
    array of indices (one per dimension) of the element displayed in the
    top-left corner of the array indicator. the length of this 1D array is the
    number of dimension of the referenced array.
    Jean-Pierre Drolet
    LabVIEW, C'est LabVIEW

  • How to clear two dimension array and content in JTextArea

    hi all....
    i have a problem to clear the counter in JTextArea..currently i design a program to store all the student details in two dimensian array...when i click "RESET"button..it will only reset the textarea..but can not clear the actual content inside the array..and when i click "Print Button" again..it's supposed nothing to display..but it displayed the thing which i previously entered......
    so i think i need to clear the array...i use append to insert the text...,for this i also can not reset...
    which the code is shown below :
    txtArea.append(msg1);
    the coding for reset area as below :
    txtArea.setText("")
    Anyone can help me?????
    thks...

    How are you trying to reset the array?, you can just creat a new array.
    Noah

  • Help me!! 2 dimensions array  - initialization

    public class DotPlot_Student
         final static int SIZE = 10;               //Maximum length of the sequence
         static int [][] match = new int [SIZE][SIZE];
         static char [][] overlay = new char [SIZE][SIZE];
         static String xStr = "CCCAA";     //test 1
         static String yStr = "CCAAA";     //test 2
         static char x[] = new char [SIZE];
         static char y[] = new char [SIZE];
         //Sliding window size and stringency
         static int winSize = 3;
         static int stringency = 2;
         //Initilization of array
         private static void init()
              for (int i = 0; i < SIZE; i++)
                   for (int j = 0; j < SIZE; j++)
                        //What is need to be initialized?
                        //at least 2 lines of code.
                        //can someone help me solve?
    }

    static int [][] match = new int [SIZE][SIZE];The array is already initialized, you don't need to do anything more.

  • How to create a Double dimension array dynamically

    Hello,
    If I have the following code running...
    String[][] str = new String[1][3];
    str[0]0] = "AAA";
    str[0][1] = "BBB";
    str[0][2] = "CCC";
    I want to do the same code using runtime object creation. i.e.
    String class_name = "java.lang.String";
    How will I write the above mentioned code without using the hard code String class, i.e. I have to use class_name instead of "String".
    Please help me.
    Thanks in advance,
    Anmol

    Thanks Drake,
    I got the solution i.e.
    String class_name = "java.lang.String";
    String ele_type = "java.lang.String";
    int ele_size = 3;
    String s1 = new String("AAA");
    String s2 = new String("BBB");
    String s3 = new String("CCC");
    ArrayList ar = new ArrayList();
    ar.add(s1);
    ar.add(s2);
    ar.add(s3);
    Object base_obj = java.lang.reflect.Array.newInstance(Class.forName(class_name), new int[]{1,ele_size});
    Object child_obj = java.lang.reflect.Array.newInstance(Class.forName(ele_type), ele_size);
    for(int i=0;i<ele_size;i++){
    Array.set(child_obj, i, ar.get(i));     
    Array.set(base_obj,0,child_obj);               
    // base_obj object is same as str shown in the above code
    Thanks,
    Anmol

  • How to use ArrayList to represent muti-dimension array?

    For example, how to use ArralyList to represent array likes this:
    a[0][0] = xxx
    a[1][1] = xxx
    a[2][2] = xxx
    .....Thanks

    For example, how to use ArralyList to represent array likes this:
    > a[0][0] = xxx
    a[1][1] = xxx
    a[2][2] = xxx
    .....Use an ArrayList populated with ArrayLists?
    kind regards,
    Jos

  • Array dimension

    I am passing a two dimension array to the function in which second dimension is always same(means no of column) that is four.
    First dimension is variable that is four or five or what so ever.(means number of rows are variable).
    In the function I have to loop through the number of rows.
    So is there any array function to find the length of first dimension.
    e.g
    some time the array is arr[2][4]
    some time it is arr[8][4]
    I want to get array first dimension that is here 2 or 8.

    Length shouldn't (can't) be capitalized if you want it to work.
    What are you doing with arrcnt? Java is not C. You can't address 2-D arrays as 1-D arrays. You need an inner loop instead of your if (k%4==0).
    for(int k=0;k<arr.length;k++)
       for(int col=0;col<arr[k].length;col++) // could use 4 instead of arr[k].length
            //do whatever with:
           arr[k][col] = ...;
    }

Maybe you are looking for

  • Transferring images from a PC to a Mac

    Fist of all, please excuse me if my questions appear elementary but I am a recent convert to Apple and to digital photography. I have a PC desk top computer and a just-purchased MacBook Pro. At the moment all of my images are on the internal hard dri

  • How can I change the color of a object inside a symbol?

    Hello! I'm working on this study and I need to change the color of an object inside a symbol when I click another object. The object is called "bola", wich is inside the symbol "ponto" and the clicking object are the colored pencils (each pencil shou

  • How do I ignore colours specified on web pages so I can use a screen tinter program?

    I have problems with my eyesight and have started using Screen Tinter software to change the colour of background to something more comfortable. I have been told that you can do the same on web pages by going to tools/internet options/accessibility/i

  • Wired Hotspot portal redirect fails

    I'm working on wired guest access from a 2960-X switch stack running 15.0(2)EX4.  The ISE 1.3 policy delivers the access-accept with the redirect URL, but the switch doesn't seem to do anything with it.  The client can do DNS resolution, so there is

  • Problem in Downloading Word Document of very huge size

    Hi Folks, I upload a word document of 5 MB size to the server. I do hexencoding before I upload. The problem is when i try to download the file from the server it goes on parsing the document and it does'nt stop. It is getting stuck in the while loop