• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

question related to ragged array

 
Ranch Hand
Posts: 151
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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


 
Ranch Hand
Posts: 1258
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
 
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Greenhorn
Posts: 13
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator


Alternatively

 
Ernest Friedman-Hill
author and iconoclast
Posts: 24207
46
Mac OS X Eclipse IDE Chrome
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Ranch Hand
Posts: 124
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
author
Posts: 14112
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • Quote
  • Report post to moderator
...sorry; I have to stop doing this post and then correct thing..

 
reply
    Bookmark Topic Watch Topic
  • New Topic