wood burning stoves*
The moose likes Beginning Java and the fly likes question related to ragged array Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Beginning Java
Bookmark "question related to ragged array" Watch "question related to ragged array" New topic
Author

question related to ragged array

ganesh pol
Ranch Hand

Joined: Apr 29, 2005
Posts: 151
friends i have problem related to ragged array
in ragged array
we create arrays of variable column length
eg

1
1 1
1 1 1
but how to create array which gives variable row length
1
1 1
1 1 1


Nathaniel Stoddard
Ranch Hand

Joined: May 29, 2003
Posts: 1258


Nathaniel Stodard<br />SCJP, SCJD, SCWCD, SCBCD, SCDJWS, ICAD, ICSD, ICED
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

ganesh --

I don't think Mr. Stoddard is answering the question you asked, so I'll give it a try. Java "2D" arrays aren't really 2D, as you seem to know: a 2D array is a 1D array of column arrays. The number of columns is thus fixed, but the number of "rows" in each column can vary. You're asking, I think, if you can make an array with a fixed number of rows, but a different number of columns in each row. The answer, strictly, is no. But I can't think of a situation where this is really going to be a problem.

Is this just idle curiosity?


[Jess in Action][AskingGoodQuestions]
ganesh pol
Ranch Hand

Joined: Apr 29, 2005
Posts: 151
bu suppaose if i have to really print some thing in which i have to change length of each row constant and keep changing column length can any one explain me it's code or give it in java
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

I don't understand what you're getting at. In any case, just store the data the other way round: put your "rows" in the columns, and your "columns" in the rows.
Kashif Iqbal Khan
Greenhorn

Joined: Dec 22, 2001
Posts: 13


Alternatively



Kashif I. Khan
Ernest Friedman-Hill
author and iconoclast
Marshal

Joined: Jul 08, 2003
Posts: 24183
    
  34

Mr. Khan,

Ummmm...

Note that I described a 2D array as being an array of columns. All you've done is to change the terms we're using; you're calling the columns "rows" and the rows "columns." You're certainly free to do that if you'd like -- that's what I was trying to say to Ganesh. But then you can no longer make an array with a fixed number of columns and a variable number of rows.
KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
If the structure of your arrays is truly important to you, perhaps the solution lies with linked lists? You could either have a LinkedList of LinkedList where each link in the list consists of another list or more simply an array of LinkedList. Then you can make it as 'ragged' as you like!
KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
... although thinking about it a bit more; it depends on whether it is acceptable to mimic a primitive data structure using an Object structure. I don't know. Obviously, you have to use method calls rather than assignment.

Also, it would be better to use a LinkedHashMap instead of a LinkedList as one of the key abilities of an array would seem to be its O(1) access time. This would give both relatively fast iterative and random access.
Ilja Preuss
author
Sheriff

Joined: Jul 11, 2001
Posts: 14112
Originally posted by KR Campbell:

Also, it would be better to use a LinkedHashMap instead of a LinkedList as one of the key abilities of an array would seem to be its O(1) access time. This would give both relatively fast iterative and random access.


An ArrayList would be better - it also gives you O(1) indexed access, without requiring you to manage the keys. In fact it is likely to be faster than a HashMap in absolute terms.


The soul is dyed the color of its thoughts. Think only on those things that are in line with your principles and can bear the light of day. The content of your character is your choice. Day by day, what you do is who you become. Your integrity is your destiny - it is the light that guides your way. - Heraclitus
KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
Originally posted by Ilja Preuss:


An ArrayList would be better - it also gives you O(1) indexed access, without requiring you to manage the keys. In fact it is likely to be faster than a HashMap in absolute terms.


..Even better then ; but do you need to manage keys with the HashMap given that we are talking about integers? Can't you just wrap them in an Integer given that ?
KR Campbell
Ranch Hand

Joined: Mar 26, 2004
Posts: 124
...sorry; I have to stop doing this post and then correct thing..

 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: question related to ragged array