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.
Hi, my name is Rommel Rico. I work as a Sr. Programmer Analyst at the University of San Diego. My passions include entrepreneurship, computers, languages, football (soccer), art, reddit, theater, science fiction, classical music, aviation, running, and video games. At the moment, I'm really into StarCraft 2.
Sparks
3 Jul, 2010
Again, thanks! That was a pretty good example.
Roberto Immerman
23 Feb, 2012
Informative article, exactly what I needed.
John Chair
24 Feb, 2012
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.
Davis Cofran
25 Feb, 2012
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.
extra large bean bag chairs
26 Feb, 2012
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.
Riley Huhtasaari
26 Feb, 2012
I think other website owners should take this site as an model, very clean and wonderful user friendly style.
Motorcycles For Sale
27 Feb, 2012
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!
Donn Corippo
1 Mar, 2012
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.
Duane Dinsmoor
2 Mar, 2012
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.
Malik Clover
3 Mar, 2012
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.
HelloKitty
4 Mar, 2012
You are a very capable individual!
Marci Bleiler
5 Mar, 2012
Its wonderful as your other posts
, thanks for posting.
Charley
6 Mar, 2012
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
Caridad Varin
6 Mar, 2012
You got a very great website, Glad I detected it through yahoo.
Sandee Forslund
9 Mar, 2012
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.
Izzykonto opinie
12 Mar, 2012
Perfect design! How did you get this layout?
เกมส์ ต
13 Mar, 2012
stays on topic and states valid points. Thank you. 408882
Sergio Bruff
14 Mar, 2012
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
Darmowe konto dla dzieci
16 Mar, 2012
Nice design! How did you get that awesome layout?
helicopter game download
17 Mar, 2012
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!!
Konto
17 Mar, 2012
Beatiful blog! Where did you get that awesome layout?
Leif Krauskopf
18 Mar, 2012
I like this web site so much, saved to bookmarks.
simulateur pret immobilier
18 Mar, 2012
I take pleasure within the comments on this weblog, it actually gives it that community feel! 459669
early childhood special education
19 Mar, 2012
Hi my friend! I want to say that this post is remarkable, good written and incorporate approximately all significant infos. I
Tarzan vibrator
19 Mar, 2012
Oh my goodness! a amazing post dude. Thanks a good deal Nevertheless I is going to be experiencing trouble with ur rss . Don
hardwood floor cost
20 Mar, 2012
Some genuinely good stuff on this internet internet site , I it. 578584
emu360v1.4
20 Mar, 2012
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.
Erica
20 Mar, 2012
A better magazine theme will make the blog looks nicer:)
เกมส์ทําอาหารมากมาย
21 Mar, 2012
Your blog has the same post as another author but i like your better.~:;
iphone 4s unlocked
30 Apr, 2012
I recognize there exists a lot of spam on this weblog. Do you want support cleansing them up? I could support amongst courses! 966265
Software Download Blog
12 May, 2012
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