DevX Home Today's Headlines   Articles Archive   Tip Bank   Forums

# Thread: Array Q.

1. Registered User
Join Date
May 2005
Location
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. Senior Member
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. 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.

4. Registered User
Join Date
May 2005
Location
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. Senior Member
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. 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();

7. Registered User
Join Date
May 2005
Location
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. Senior Member
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. ## 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();
}
}```
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```

10. Registered User
Join Date
May 2005
Location
Posts
173

## Thank you

I like the Demo.
Thank you,

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

 FAQ Latest Articles Java .NET XML Database Enterprise
 Questions? Contact us. C++ Web Development Wireless Latest Tips Open Source