d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Code Styling Question
Add Reply New Topic New Poll
Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
Jan 5 2014 02:49pm
I am looking for comparisons between parallel arrays, 2d arrays, and an array with objects for this problem:

I am storing 100 item numbers and their available quantities. I will want to sort the numbers and combine item numbers that are the same.

What data structure would you use to solve this problem? I do not need code examples or instructions on how do build anything. I am looking for discussion on what is better/faster, not code.

Thanks
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
Jan 5 2014 03:07pm
Rule of thumb, list (or other collection/datastructure) of objects is preferred in OOP.

Parallel arrays are almost always a bad idea, especially if you're sorting n arrays based on another's index.

2D arrays have no place here. one obvious reason is the datatype restriction. what if item numbers aren't necessarily integers (eg: UPC)? and the more columns you add, the more likely you'll need different datatypes. But more importantly, you shouldn't be associating different attributes in an array like that unless it's necessary.

as for "faster", it's irrelevant for an example like this.

This post was edited by carteblanche on Jan 5 2014 03:14pm
Member
Posts: 11,610
Joined: Oct 28 2008
Gold: 1,795.00
Jan 5 2014 06:25pm
multi dimension array, look into PHP's usort
Member
Posts: 42
Joined: Jan 11 2014
Gold: 3,097.85
Jan 11 2014 11:14am
If the only thing you want to associate with each item number are the quantities, then you should probably go with some type of associative array where the key is the item number and the value is the quantity. If you're really worried about performance, maybe a hash table is the way to go (I think that's generally the default associative array type?). This makes looking up an item's quantity much faster, but the size of the hash table structure is much larger than a regular array or 2d array.

If you have more than just quantities with the items, you couldl do an associative array where the key is the item number and the value is the item object (with quantity and other attributes).

This post was edited by Kronosaurus on Jan 11 2014 11:20am
Member
Posts: 5,269
Joined: Oct 18 2006
Gold: 21,400.00
Jan 11 2014 07:41pm
When I said "better/faster" I was talking about coding time and simplicity to modify and understand quickly. With only 100 items, performance is irrelevant as carteblanche said.
Go Back To Programming & Development Topic List
Add Reply New Topic New Poll