How do you use array.indexOf for class arrays with int, char and strin

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 11691 Views - Last Post: 21 December 2012 - 05:44 PM Rate Topic: -----

#1 wholegrain   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-March 12

How do you use array.indexOf for class arrays with int, char and strin

Posted 17 March 2012 - 11:41 AM

Is there some sort of default method in the array class? I know how to make a sequential search, but with System.Array? I am not sure what they mean by that.
Is This A Good Question/Topic? 0
  • +

Replies To: How do you use array.indexOf for class arrays with int, char and strin

#2 immeraufdemhund   User is offline

  • D.I.C Regular

Reputation: 79
  • View blog
  • Posts: 495
  • Joined: 29-March 10

Re: How do you use array.indexOf for class arrays with int, char and strin

Posted 17 March 2012 - 11:52 AM

are you referring to foreach?
Was This Post Helpful? 0
  • +
  • -

#3 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6532
  • View blog
  • Posts: 14,447
  • Joined: 02-June 10

Re: How do you use array.indexOf for class arrays with int, char and strin

Posted 17 March 2012 - 11:57 AM

You use a loop (for, foreach) and check each element to see if it is what you are looking for.

int[] myArray = new array[10];
foreach(int yogibear in myArray)
{
    if (yogibear == 7) Console.WriteLine("We have a winner!")
}


View Postwholegrain, on 17 March 2012 - 12:41 PM, said:

I am not sure what they mean by that.


Ah... The mysterious 'they'. They are almost as bad as 'them'. I never trust either one.
Was This Post Helpful? 0
  • +
  • -

#4 wholegrain   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-March 12

Re: How do you use array.indexOf for class arrays with int, char and strin

Posted 17 March 2012 - 12:18 PM

How do you use array.indexOf for class arrays with int, char and string variables?


int indice = Array.IndexOf(station, new Station("York", 0, 0));


I think you can use 0 for int variables, but what about char variables for sex?

This post has been edited by tlhIn`toq: 17 March 2012 - 12:26 PM
Reason for edit:: Please use code tags

Was This Post Helpful? 0
  • +
  • -

#5 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6532
  • View blog
  • Posts: 14,447
  • Joined: 02-June 10

Re: How do you use array.indexOf for class arrays with int, char and strin

Posted 17 March 2012 - 12:29 PM

Rather than opening 30 threads for every step of your homework on arrays, how about keeping it to just the one since all the questions are for the same thing? I'll merge this one with the other one:
http://www.dreaminco...1&#entry1579010

View Postwholegrain, on 17 March 2012 - 01:18 PM, said:

How do you use array.indexOf for class arrays with int, char and string variables?


This is well documented on the MSDN
http://msdn.microsof...ay.indexof.aspx

That should always be your first stop when using new objects/methods/types for the first time.

Read up on it. Then give it a try. We learn so much more by trial and error than asking to be taken by the hand.

Suggestion:
Don't replace lines of code that don't work. Instead comment them out and put your new attemps below that. This will keep you from re-trying the same ideas over and over. Also, when you come back to us saying "I've tried this 100 different ways and still can't get it", we can actually see what you tried. So often a failed attempt is very very close and just needs a little nudge in the right direction. So if we can say "See what you did in attempt 3... blah blah" it helps a lot

Spoiler

If you are using Visual Studio you can select a block of lines and hit control+k control+c (Kode Comment) to comment it out. control+k control+u (Kode Uncomment) to uncomment a selected block.



Finding answers to specific problems:
Spoiler

This post has been edited by tlhIn`toq: 17 March 2012 - 12:31 PM

Was This Post Helpful? 0
  • +
  • -

#6 wholegrain   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-March 12

Re: How do you use array.indexOf for class arrays with int, char and strin

Posted 17 March 2012 - 12:53 PM

Yeah, but Microsoft examples only cover 1 dimensional arrays.
Was This Post Helpful? 0
  • +
  • -

#7 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6532
  • View blog
  • Posts: 14,447
  • Joined: 02-June 10

Re: How do you use array.indexOf for class arrays with int, char and strin

Posted 17 March 2012 - 01:05 PM

Quote

Yeah, but Microsoft examples only cover 1 dimensional arrays.

So what does that tell you about array.indexof?
That it is built for single-dimension arrays and therefore is not the solution you are looking for. Go back to looping through the collection like we pointed out to you.

int indice = Array.IndexOf(station, new Station("York", 0, 0));



If you look at this and think about it a second, you can see that this logic can never work. You are making a brand new Station object. Then wanting to see what it's index is within your array of Stations. The new instance doesn't exist in the array: Its never been added to the array. There might be another instance of the same type, with the same values, but it is not the same instance. It just resembles the instance you just made.

This post has been edited by tlhIn`toq: 17 March 2012 - 01:09 PM

Was This Post Helpful? 0
  • +
  • -

#8 wholegrain   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 11
  • Joined: 17-March 12

Re: How do you use array.indexOf for class arrays with int, char and strin

Posted 17 March 2012 - 01:35 PM

Nevermind, found the solution. Thanks anyway.
Was This Post Helpful? 0
  • +
  • -

#9 tlhIn`toq   User is offline

  • Xamarin Cert. Dev.
  • member icon

Reputation: 6532
  • View blog
  • Posts: 14,447
  • Joined: 02-June 10

Re: How do you use array.indexOf for class arrays with int, char and strin

Posted 17 March 2012 - 01:37 PM

In the spirit of helping out the next person that finds this thread via Google, how about sharing the solution you found?
Was This Post Helpful? 0
  • +
  • -

#10 raghavh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-December 12

Re: How do you use array.indexOf for class arrays with int, char and strin

Posted 20 December 2012 - 10:54 PM

View Postwholegrain, on 17 March 2012 - 01:35 PM, said:

Nevermind, found the solution. Thanks anyway.


View Postwholegrain, on 17 March 2012 - 01:35 PM, said:

Nevermind, found the solution. Thanks anyway.

Hello wholegrain. Can you share the solution with us?
Was This Post Helpful? 0
  • +
  • -

#11 Momerath   User is offline

  • D.I.C Lover
  • member icon

Reputation: 1020
  • View blog
  • Posts: 2,463
  • Joined: 04-October 09

Re: How do you use array.indexOf for class arrays with int, char and strin

Posted 20 December 2012 - 11:00 PM

I'd use FindIndex personally.
Was This Post Helpful? 0
  • +
  • -

#12 raghavh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-December 12

Re: How do you use array.indexOf for class arrays with int, char and strin

Posted 21 December 2012 - 02:30 AM

View PostMomerath, on 20 December 2012 - 11:00 PM, said:

I'd use FindIndex personally.

FindIndex doesnot exist in framework 1.1. We are using 1.1 framework.
Was This Post Helpful? 0
  • +
  • -

#13 Skydiver   User is offline

  • Code herder
  • member icon

Reputation: 6166
  • View blog
  • Posts: 21,273
  • Joined: 05-May 12

Re: How do you use array.indexOf for class arrays with int, char and strin

Posted 21 December 2012 - 06:13 AM

You can just use Reflector or dotPeek to look at the implementation of FindIndex() and use the same concept when writing your solution in .NET Framework 1.1. Personally, I think it would be faster simply write your own code to loop over the array until the item is found. While looping keep track of the index and return than when you find the item. It's not that difficult. You can probably write it faster than it will take you time to download Reflector, install it, and find the function that you want to reflect.

This post has been edited by Skydiver: 21 December 2012 - 06:14 AM

Was This Post Helpful? 1
  • +
  • -

#14 raghavh   User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 2
  • Joined: 20-December 12

Re: How do you use array.indexOf for class arrays with int, char and strin

Posted 21 December 2012 - 06:44 AM

View PostSkydiver, on 21 December 2012 - 06:13 AM, said:

You can just use Reflector or dotPeek to look at the implementation of FindIndex() and use the same concept when writing your solution in .NET Framework 1.1. Personally, I think it would be faster simply write your own code to loop over the array until the item is found. While looping keep track of the index and return than when you find the item. It's not that difficult. You can probably write it faster than it will take you time to download Reflector, install it, and find the function that you want to reflect.

We have used looping to find the item we are looking for. But we want to optimize our code hence was looking for the solution.
Was This Post Helpful? 0
  • +
  • -

#15 Curtis Rutland   User is offline

  • (╯□)╯︵ (~ .o.)~
  • member icon


Reputation: 5103
  • View blog
  • Posts: 9,283
  • Joined: 08-June 10

Re: How do you use array.indexOf for class arrays with int, char and strin

Posted 21 December 2012 - 04:38 PM

Well, all my recent posts in this thread were lost. So I'll repeat them.

Optimization would depend on what you're searching for (what type, how is the array structured, etc). At worst, a linear search is O(n), which isn't all that inefficient.

Also, the .NET implementation of FindIndex is a linear search; a simple loop.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2