0

Is it possible to change some parameter of class inside of Vector:

  class CGen{
    public String Par1 = "";
    public String Par2 = "";
  }

  Vector GenVector = new Vector(0);
  //....

I need to do using the simplest form...

  //....
  CGen NewGen = new CGen();
  GenVector.addElement(NewGen);

  void ChangeItemGen(int Index, String Str) {
    GenVector.elementAt(Index).Par1 = Str;
  }

The code above is very simple but the real code is more complex.

MSalters
  • 167,472
  • 9
  • 150
  • 334
Anita
  • 1,217
  • 1
  • 14
  • 27

1 Answers1

0

It is possible, but not quite as simple as in the example. You have to either cast the result of elementAt to the correct type so that the compiler knows about the Par1 variable:

((CGen) GenVector.elementAt(Index)).Par1 = Str;

Or you have to specify the element type you use with GenVector when you declare it, then you don't need an explicit cast anymore:

Vector<CGen> GenVector = new Vector<CGen>(0);
....
GenVector.elementAt(Index).Par1 = Str;

You probably should be using ArrayList instead of Vector, unless you are maintaining a program written in the 90's. See Why is Java Vector class considered obsolete or deprecated?

Community
  • 1
  • 1
Joni
  • 105,306
  • 12
  • 136
  • 187