Array Q.


DevX Home    Today's Headlines   Articles Archive   Tip Bank   Forums   

Results 1 to 10 of 10

Thread: Array Q.

  1. #1
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    Array Q.

    Hi to all,
    According to Data Structures and Algorithms in JAVA,
    int[] a = new int[10];
    float[][] x = new float[8][10];
    i = 5;
    a[i] = 138;
    x[i][i+1] = 2.189 + x[i][i];
    i= a.length;
    j = x[4].length;\\ What does it do? Only this line.

  2. #2
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    because the second dimension of the x 2D array is an array (another way to put it, you are storing an array of a certain length as the element which is stored in each element of x), you are assigning to j the length of the array which is the element stored at x[4]

  3. #3
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    In other words, though not necessarily simpler:
    It assigns the value 10 to j. The answer would be the same for all x[0] -> x[7]. X is a 8x10 matrix.
    eschew obfuscation

  4. #4
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173
    Thank you both.
    Now, I understand that: j=x[4].length is puting the length of the fifth element in x to j.
    I hope that I am right.
    Last edited by Kinda Electroni; 01-13-2007 at 09:51 AM.

  5. #5
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    Remember that "length" is not a built-in field for every object which might be stored in an array ... it is available only for an array. If you had an array of lists you would not have a value for length in response to the statement x[4].length.

  6. #6
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560
    Kinda Electroni:
    Just a minor thing.... x[4] is the fifth element

    For an array of lists, like:

    ArrayList [] x=new ArrayList[10];

    you would use the size() method, like:

    j = x[4].size();
    eschew obfuscation

  7. #7
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    The Array is the Array

    Hi everyone, and thank you both,
    My background is HARDWARE, but the Array is the Array in Math, Hardware, Software,
    Now, when I declare a two dim. Array (The length of any elem. in that array is the same) each elem. is an array that has the length of the number of columns in the main array. (Am I right?)
    Why he is trying to call the length of the x[4]?
    Meanwhile it has been mentioned that the .length is not internal, which means I could not call it for x[4].length.
    i.e. j=x [4].length is not valid?
    It is in Page 33 of 3ed Data Structure and Algorithms in Java, by Michael T. Goodrich and Roberto Tamassia.
    I am sorry to have thick brain.
    What can I do?

  8. #8
    Join Date
    Dec 2004
    Location
    San Bernardino County, California
    Posts
    1,468
    If you are referring to what I wrote, it appears that you have misunderstood what was written. Arrays have an internal field which is "length". We do not have to write or call a method to get to the value held in that field. None of the Collections (Map, Set, Deque, Queue, List, etc.) have an internal field "length" which is available to us. We need to add a size or capacity field/or it is added in the implementation.

    The original inquiry related to a 2D array and you were trying to discern what "length" was returned from a reference to the object stored at x[4] - length is the length of the array held/stored/referenced by your code's calling x[4].

  9. #9
    Join Date
    Nov 2004
    Location
    Norway
    Posts
    1,560

    Trying to clarify

    You are using a "fuzzy" word here; "call". Calling is, at least in my book, invoking a function/method/subroutine (many words for same thing). The statement:

    j=x [4].length;

    .. is a perfectly valid assignment. It doesn't call anything, it just copies the value of the length field in the array x[4] to the variable j.

    A 2D array in java (and c/c++) is an array of arrays. It doesn't need a fixed 2nd dimension. Consider the following code:

    Code:
    public class ArrayDemo {
      int [][] twoDArray=new int [5][]; // note the unitilialized 2nd. dim
      public ArrayDemo() {
        assignRows();
      }
      public void show() {
        for (int i=0; i<twoDArray.length; i++) {
          System.out.println("twoDArray["+i+"].length="+twoDArray[i].length);
        }
      }
      private void assignRows() {
        twoDArray[0] = new int[4];
        twoDArray[1] = new int[5];
        twoDArray[2] = new int[6];
        twoDArray[3] = new int[7];
        twoDArray[4] = new int[8];
      }
      public static void main(String[] args) {
        ArrayDemo ad = new ArrayDemo();
        ad.show();
      }
    }
    Yielding the following output:
    Code:
    twoDArray[0].length=4
    twoDArray[1].length=5
    twoDArray[2].length=6
    twoDArray[3].length=7
    twoDArray[4].length=8
    eschew obfuscation

  10. #10
    Join Date
    May 2005
    Location
    Ontario, Canada
    Posts
    173

    Thank you

    I like the Demo.
    Thank you,

Similar Threads

  1. Replies: 26
    Last Post: 12-01-2012, 04:12 AM
  2. Reversing an Array
    By Tmcclain in forum Java
    Replies: 7
    Last Post: 02-13-2009, 10:57 PM
  3. redim help !!!
    By angelito in forum VB Classic
    Replies: 1
    Last Post: 11-21-2005, 06:16 AM
  4. Dynamically allocating a 2d array
    By nnp in forum C++
    Replies: 6
    Last Post: 11-01-2005, 09:05 AM
  5. Replies: 2
    Last Post: 04-15-2005, 09:06 PM

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


   Development Centers

   -- Android Development Center
   -- Cloud Development Project Center
   -- HTML5 Development Center
   -- Windows Mobile Development Center