Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Which Delphi data structure can hold a list of unique integers?

When I approach Java problems, I use the collection pattern. However, doing it in Delphi is quite a nightmare since there is no Integer object to handle things.

I need a data structure that holds numbers. I want to be able to add numbers, remove numbers, and check the contents of the collection, and each number must be unique.

I'm not interested in a solution I need to implement and test for bugs myself. Is there a ready object like Java's HashTable?

like image 744
none Avatar asked Oct 12 '10 11:10

none


1 Answers

I know it's dirty, but you could misuse TStringList (or THashedStringList).

var
  numberList: TStringList;
begin
  numberList := TStringList.Create;
  numberList.Duplicates := dupIgnore;
  numberList.Sorted := true;
  numberList.add(IntToStr(1));
  numberList.add(IntToStr(2));
  numberList.add(IntToStr(3));
  numberList.add(IntToStr(1));
  // numberList.CommaText = '1,2,3'
like image 145
splash Avatar answered Sep 22 '22 15:09

splash