Multidimenitonal Arrays
-
I just got into school for Computer Information Systems, and I am currently in my first programming class (ROOKIE!!!) I got bored with the assignments they were giving me so I decided to start my own project. However I am running into a problem. I created a StreamReader() in order to read a .txt file and distribute the contents into a [250,4] array. Now I've got all of that working, but the array is not completely filled. There is more to be added during the execution of the program. Now to the point. I am needing to search the first column of the array for a spacific value, and have it return the subscript so that I can access the value of the 4th column at that subscript. Now the only method that I have been shown for this is BinarySearch(); but for this to work the array must be in ascending order. Come to find out the Array.Sort() method only works on one-dimentional arrays... So how would I accomplish sorting the multidimentional array so that I keep all of the information in it's appropriate 'row'? Or if I can't sort it, is there another method that might serve the same purpose? Any help would be greatly appreciated! (Please keep it reletively simple, I'm still gettin' familiar with the terminology) Eric
-
I just got into school for Computer Information Systems, and I am currently in my first programming class (ROOKIE!!!) I got bored with the assignments they were giving me so I decided to start my own project. However I am running into a problem. I created a StreamReader() in order to read a .txt file and distribute the contents into a [250,4] array. Now I've got all of that working, but the array is not completely filled. There is more to be added during the execution of the program. Now to the point. I am needing to search the first column of the array for a spacific value, and have it return the subscript so that I can access the value of the 4th column at that subscript. Now the only method that I have been shown for this is BinarySearch(); but for this to work the array must be in ascending order. Come to find out the Array.Sort() method only works on one-dimentional arrays... So how would I accomplish sorting the multidimentional array so that I keep all of the information in it's appropriate 'row'? Or if I can't sort it, is there another method that might serve the same purpose? Any help would be greatly appreciated! (Please keep it reletively simple, I'm still gettin' familiar with the terminology) Eric
Eric (eD) wrote:
Now the only method that I have been shown for this is BinarySearch(); but for this to work the array must be in ascending order.
You don't need to use a method for searching. Just loop throught the items and look for the value. If the rows in the array have different meaning, perhaps you shouldn't use an array at all, but an object oriented solution like a list of objects. If you want to locate the items by a specific value, you can use a dictionary of objects. Example:
// create a dictionary Dictionary<int, Person> persons = new Dictionary<int, Person>; // add persons to the dictionary persons.Add(7, new Person("Paul", "Simon")); persons.Add(42, new Person("Art", "Garfunkel")); // find a person by key Person art = persons[42];
--- single minded; short sighted; long gone;
-
Eric (eD) wrote:
Now the only method that I have been shown for this is BinarySearch(); but for this to work the array must be in ascending order.
You don't need to use a method for searching. Just loop throught the items and look for the value. If the rows in the array have different meaning, perhaps you shouldn't use an array at all, but an object oriented solution like a list of objects. If you want to locate the items by a specific value, you can use a dictionary of objects. Example:
// create a dictionary Dictionary<int, Person> persons = new Dictionary<int, Person>; // add persons to the dictionary persons.Add(7, new Person("Paul", "Simon")); persons.Add(42, new Person("Art", "Garfunkel")); // find a person by key Person art = persons[42];
--- single minded; short sighted; long gone;
Didn't know that was possible with dictionaries, that's cool! I would probably create a custom class (like you did with Person) and use ArrayList or something similar. I would only use old-school multi-dim arrays if performance was a real issue. But with 250x4 I don't think it can be ;) g'luck
Visual Studio can't evaluate this, can you?
public object moo { __get { return moo; } __set { moo = value; } }
-
Eric (eD) wrote:
Now the only method that I have been shown for this is BinarySearch(); but for this to work the array must be in ascending order.
You don't need to use a method for searching. Just loop throught the items and look for the value. If the rows in the array have different meaning, perhaps you shouldn't use an array at all, but an object oriented solution like a list of objects. If you want to locate the items by a specific value, you can use a dictionary of objects. Example:
// create a dictionary Dictionary<int, Person> persons = new Dictionary<int, Person>; // add persons to the dictionary persons.Add(7, new Person("Paul", "Simon")); persons.Add(42, new Person("Art", "Garfunkel")); // find a person by key Person art = persons[42];
--- single minded; short sighted; long gone;
First off, Thanks for your responses! From what I understand, the above looks like it would work. The problem is, I don't know where to start...? (I'm a complete rookie at this) Does anyone know where I can find some sort of instructional for dictionaries? or possibly just a breakdown of the above example? Thanks again.