Rommel Rico: Random ramblings

How jagged arrays can be used and when they would be preferred over normal arrays

This is a follow-up from this post. A reader asks:

Could you possibly do a post on how jagged arrays can be used and when they would be preferred over normal arrays? Thanks!

Sure, why not!? I encourage people to ask me questions as I sometimes run out of blog topics.

As I’m sure you already know, the array is one of the most common data structure in programming. There are usually two types of arrays, rectangular and jagged arrays. Rectangular arrays can usually be thought of as a an M by N matrix like this:

You can use rectangular arrays to store data, which can then be used in literally infinite amounts of ways. An example would be a table of student’s grades, with M being student IDs and N being their grades.

On the other hand, jagged arrays are arrays that are not rectangular in “shape”; that is, at least one row has a different amount of columns than the rest. Online and on textbooks you will see people call them “arrays of arrays”, but personally I find this confusing. Just look at this picture instead:

This is a jagged array example

Now, an example of when you might want to use a jagged array? I don’t have code for it, but let’s continue with our student’s grades example. Say you have 10 students in a class, and 4 test grades. Ideally, you would have a rectangular array of 10*4 = 40 fields. Now say some students miss class and some other students earn extra credit, then you could not a rectangular shaped array. You could make an even bigger array where all the grades would fit, but this has the potential to be inefficient. Why make a table larger than it has to be?

The above is kind of confusing, so I’ll illustrate it. Say you have 4 students and 4 tests, but one students drops class and another gets extra credit. Then, you have a table like this:

A    B    A    A
C     D
A    A    A    A    A
B     A    B    A

To take in all the scores in a rectangular array, you would need a 4 by 5 array because otherwise we couldn’t fit student#3′s scores. This allocates memory for 20 fields, even though we only need 15! This may not seem like a big deal, but imagine this table is now millions of rows long and imagine you have to visit each field to calculate class averages. This would clearly result in performance issues because the computer spends time visiting empty fields. In any case, with jagged arrays we can create tables of exactly the size we want. As far as I know, that’s all the benefit there is to them.

Because they are so confusing, I avoid them like the plague. You should use cleaner things like linked lists.

  • Again, thanks! That was a pretty good example.

  • Informative article, exactly what I needed.

  • hey :) what theme do you think you’re working with for this 1? (this actual site) I kinda like this and I used to be pondering if it is out there.

  • I’m not sure exactly why but this weblog is loading extremely slow for me. Is anyone else having this issue or is it a problem on my end? I’ll check back later and see if the problem still exists.

  • hey :) what design are you currently applying for this one in particular? (this actual web site) I kinda love this and I used to be thinking if it is available.

  • I think other website owners should take this site as an model, very clean and wonderful user friendly style.

  • Outstanding post however I was wanting to know if you could write a litte more on this subject? I’d be very thankful if you could elaborate a little bit further. Appreciate it!

  • I’ve recently started a website, the info you offer on this web site has helped me tremendously. Thank you for all of your time & work.

  • Thanks for an incredible submit, can study your others content. thanks for your thinking within this, I experienced a bit strike by this text. Thanks again again! You make an excellent time. Portrays natures best by the fantastic data here. I think if a greater number thought of it like that, they’d have a very better time period have the suspend ofing the matter.

  • Just desire to say your article is as surprising. The clearness on your submit is simply great and i can suppose you’re an expert on this subject. Fine with your permission let me to take hold of your feed to stay updated with coming near near post. Thanks 1,000,000 and please keep up the gratifying work.

  • You are a very capable individual!

  • Its wonderful as your other posts :D , thanks for posting.

  • Hello there, are you having difficulties with the web hosting? I had to refresh the page about million times to be able to get the page to load

  • You got a very great website, Glad I detected it through yahoo.

  • Heya i am for the primary time here. I came across this board and I in finding It really helpful & it helped me out much. I am hoping to offer something again and help others such as you helped me.

  • Perfect design! How did you get this layout?

  • stays on topic and states valid points. Thank you. 408882

  • Wonderful blog! I found it while surfing around on Yahoo News. Do you have any tips on how to get listed in Yahoo News? I’ve been trying for a while but I never seem to get there! Appreciate it

  • Nice design! How did you get that awesome layout?

  • Oh my goodness! Incredible article dude! Thank you, However I am going through troubles with your RSS. I don’t know the reason why I cannot join it. Is there anyone else getting identical RSS problems? Anyone that knows the answer will you kindly respond? Thanks!!

  • Beatiful blog! Where did you get that awesome layout?

  • I like this web site so much, saved to bookmarks.

  • I take pleasure within the comments on this weblog, it actually gives it that community feel! 459669

  • Hi my friend! I want to say that this post is remarkable, good written and incorporate approximately all significant infos. I

  • Oh my goodness! a amazing post dude. Thanks a good deal Nevertheless I is going to be experiencing trouble with ur rss . Don

  • Some genuinely good stuff on this internet internet site , I it. 578584

  • Thank you for any other informative web site. The place else may just I am getting that type of information written in such a perfect means? I’ve a mission that I’m just now working on, and I have been on the glance out for such info.

  • A better magazine theme will make the blog looks nicer:)

  • Your blog has the same post as another author but i like your better.~:;

  • I recognize there exists a lot of spam on this weblog. Do you want support cleansing them up? I could support amongst courses! 966265

  • Superb weblog here! Also your web site loads up really quickly! What host are you making use of? Can I get your affiliate link to your host? I wish my web site loaded up as quickly as yours lol xrumer 849368

You can follow any responses to this entry through the RSS 2.0 feed.