Foros Club Delphi

Foros Club Delphi (https://www.clubdelphi.com/foros/index.php)
-   Varios (https://www.clubdelphi.com/foros/forumdisplay.php?f=11)
-   -   Me sale I/O error 125, ¿qué hago mal? (https://www.clubdelphi.com/foros/showthread.php?t=88214)

bulc 30-04-2015 16:04:48

Me sale I/O error 125, ¿qué hago mal?
 
Estoy practicando con TList y este código me da ese error. Por favor, ¿me podéis decir qué está mal?
Código Delphi [-]
procedure TForm1.Button1Click(Sender: TObject);
Const
   Const_A: Array[1..6] of String =( 'a', 'b', 'c', 'd', 'e', 'f');
var
  I: Integer;
  Element: Integer;
  Lista: TList;
  Dato : String;
begin
  Memo1.Clear;
  Randomize;
  try
    Lista := TList.Create();
    try
      for I := 1 to High(Const_A) do
        begin
          Lista.Add( Pointer( I ));
        end;
     repeat
        Element := Random(Lista.Count);
        Dato := Const_A[Element];
        Memo1.Lines.Add( IntToStr (Element) +'   '+ Dato );  // Const_A[Elemento]
        Lista.Delete(Element);
      until Lista.Count = 0;
     finally
       Lista.Free;
    end;
  except
    on E: Exception do
      Writeln(E.ClassName, ': ', E.Message);
  end;
end;
Quería hacer un TList de integers, pero no lo consigo.
Gracias por vuestra ayuda.
bulc

ecfisa 30-04-2015 16:30:55

Hola bulc.

A primera vista, dado que el índice Element dentro del repeat recibe un valor aleatorio,
Código Delphi [-]
  Element := Random(Lista.Count);
  // dado Const_A, debería ser: Element := Random(Lista.Count-1)+1;
con toda seguridad obtenga dos o mas veces el mismo valor dentro del ciclo repeat.

Esto haría que en la línea,
Código Delphi [-]
Lista.Delete(Element);
intente borrar un elemento que ya fue borrado anteriormente. Provocando una excepción del tipo List index out of bound

Saludos :)

ecfisa 30-04-2015 16:39:50

Hola de nuevo.

No sé el propósito del código, pero pienso que podrías hacer algo así:
Código Delphi [-]
...
begin
  Memo1.Clear;
  Randomize;
  Lista := TList.Create();
  try
    for i := Low(Const_A) to High(Const_A) do
      Lista.Add( Pointer(i) );
    for i:= Lista.Count - 1 downto 0 do
    begin
      Element := Random(Lista.Count - 1) + 1;
      Memo1.Lines.Add(Format('%d %s', [Element, Const_A[Element]]));
    end;
  finally
    for i:= Lista.Count - 1 downto 0 do
      Lista.Delete( i );
    Lista.Free;
  end;
end;

Saludos :)


La franja horaria es GMT +2. Ahora son las 07:53:52.

Powered by vBulletin® Version 3.6.8
Copyright ©2000 - 2026, Jelsoft Enterprises Ltd.
Traducción al castellano por el equipo de moderadores del Club Delphi