File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
Win a copy of Clojure in Action this week in the Clojure forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

question related to ragged array

 
ganesh pol
Ranch Hand
Posts: 151
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 1258
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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?
 
ganesh pol
Ranch Hand
Posts: 151
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 13
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


Alternatively

 
Ernest Friedman-Hill
author and iconoclast
Marshal
Pie
Posts: 24204
34
Chrome Eclipse IDE Mac OS X
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
... 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
Posts: 14112
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
KR Campbell
Ranch Hand
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 124
  • 0
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
...sorry; I have to stop doing this post and then correct thing..

 
It is sorta covered in the JavaRanch Style Guide.
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic