-
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.
-
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]
-
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
-
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.
-
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.
-
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
-
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?
-
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].
-
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
-
Thank you
I like the Demo.
Thank you,
Similar Threads
-
Replies: 26
Last Post: 12-01-2012, 04:12 AM
-
By Tmcclain in forum Java
Replies: 7
Last Post: 02-13-2009, 10:57 PM
-
By angelito in forum VB Classic
Replies: 1
Last Post: 11-21-2005, 06:16 AM
-
Replies: 6
Last Post: 11-01-2005, 09:05 AM
-
By kanakatam in forum Java
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
Forum Rules
|
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
|
Bookmarks