5
Answers

BinarySearch problem...

tesic

tesic

20y
3.1k
1
Hello, I'm working on a small project and I have to search ArrayList that contains simple object list. This is my object: public class NameValue { private string strName; private string strValue; public NameValue( string sNameValuePair ) { StringTokenizer tok = new StringTokenizer( sNameValuePair, "=" ); this.strName = tok.nextElement().Trim(); this.strValue = tok.nextElement().Trim(); } private void splitName( StringTokenizer tok ) { this.strName = tok.nextElement(); string temp = this.strName; } public string Name { get { return strName; } set { strName = value; } } public string Value { get { return strValue; } set { strValue = value; } } } This is code where I call BinarySearch method: public ArrayList AddPairToList( string sNameValuePair ) { int nPos; NameValue nv = new NameValue( sNameValuePair ); SortByName(); nPos = a.BinarySearch( nv, new CompareCustomDataType() ); if( nPos < 0 ) { a.Add( nv ); } else { MessageBox.Show( "The item you tried to add to the list already exist!" ); } return GetCurrentList(); } And this is my CompareCustomDataType class: public class CompareCustomDataType : IComparer { public int Compare( object x, object y ) { if (x == null) return -1; if (y == null) return 1; NameValue xNameValue = (NameValue) x; NameValue yNameValue = (NameValue) y; if( xNameValue.Name.CompareTo( yNameValue.Name ) > 0 ) { return 1; } else if( xNameValue.Name.CompareTo( yNameValue.Name ) < 0 ) { return -1; } return String.Compare( xNameValue.Value, yNameValue.Value ); } } However, it doesn't work properly. Actually, my list seems like this: Australia = Canberra Austria = Wiena Canada = Toronto Canada = Ottawa ... When I try to add some pair to the list and if only unique pair already exist in the list (when I say unique pair I mean only one pair with unique name and unique value such as: Australia, Canberra) everything works fine, but when I try to add some pair to the list that already exist and if it is not unique pair (when I say "it is not unique pair" I mean I have pairs with the same name and different values such as: Canada, Toronto; Canada, Ottawa), first couple of times I get proper message "The item you tried to add to the list already exist!". After that, the object that already exist is added to the list and I have duplicate... Would you be so kind to help me to fix this problem... Thank you in advance, Goran Tesic
Answers (5)
4
Onkar Sharma

Onkar Sharma

45 31.6k 9.6m Aug 06

Remembering the code can be difficult, but there are a few things you can do to help:

  1. Practice Regularly
  2. Use Mnemonics
  3. Write Code by Hand
  4. Understand, Don’t Memorize
  5. Build Projects
  6. Break Code into Chunks
  7. Use Code Editors with Autocomplete
  8. Review and Refactor the code
  9. Educate others
  10. Stay Curious
  11. Stay Updated

A combination of these strategies can improve your ability to remember and use the code successfully. Thanks!

!! Happy Learning !!

4
Amit Mohanty

Amit Mohanty

17 52.2k 6.1m Jul 18

To remember code effectively, practice coding regularly. By consistently solving problems and building projects, you develop memory and deepen your understanding of codes.

3
Gowtham Cp

Gowtham Cp

647 1.3k 7.9k Aug 22

To remember code:

  1. Practice often - hands-on coding sticks.
  2. Understand, don’t memorize - grasp the logic.
  3. Build projects - real use makes it memorable.
  4. Review regularly - keep refreshing your memory.
  5. Use cheat sheets - Striver, Love Babbar, and others are great resources.
  6. Work on algorithms - solve similar problems to strengthen understanding.
  7. Follow structured paths - ike Coding Ninjas; once confident, move to LeetCode.
  8. Debug actively - understand the solution logic deeply so you can tweak it without memorizing.
3
Abhishek  Yadav

Abhishek Yadav

105 17.2k 346.6k Aug 06

Practice! practice! practice!

pracice makes a man perfect

2
Vilas Gite

Vilas Gite

500 2.5k 547.4k Aug 22

Hi friend,

There is no need to remember code actually; while building project tell yourself each and every code. :)