Hi everyone,


I am currently trying to add values to an element spec array dynamically.


This is what i am trying to do

SimpleAttributeSet start = new SimpleAttributeSet();
      start.addAttribute(AbstractDocument.ElementNameAttribute, "start");
 
      SimpleAttributeSet end = new SimpleAttributeSet();
      end.addAttribute(AbstractDocument.ElementNameAttribute, "end");


DefaultStyledDocument.ElementSpec esStart1 = new DefaultStyledDocument.ElementSpec(start, DefaultStyledDocument.ElementSpec.StartTagType);
      DefaultStyledDocument.ElementSpec esStart2   = new DefaultStyledDocument.ElementSpec(start,   DefaultStyledDocument.ElementSpec.StartTagType);
      DefaultStyledDocument.ElementSpec esStart3  = new DefaultStyledDocument.ElementSpec(start,  DefaultStyledDocument.ElementSpec.StartTagType);

DefaultStyledDocument.ElementSpec esEnd1 = new DefaultStyledDocument.ElementSpec(end, DefaultStyledDocument.ElementSpec.EndTagType);
      DefaultStyledDocument.ElementSpec esEnd2   = new DefaultStyledDocument.ElementSpec(end,   DefaultStyledDocument.ElementSpec.EndTagType);
      DefaultStyledDocument.ElementSpec esEnd3  = new DefaultStyledDocument.ElementSpec(end,  DefaultStyledDocument.ElementSpec.EndTagType);

DefaultStyledDocument.ElementSpec[] esArray = {
        esStart1,
        esEnd1,
          esStart2,
          esEnd2,
            esStart3,
            esEnd3};

The above code is trying to add some custom elements into the styyled document but the thing is that i do not know what amount of this element does the user require. For example if the user requires only one of this element then this is what i would add into the array

DefaultStyledDocument.ElementSpec[] esArray = {
        esStart1,
        esEnd1}

if the user wants two types of this elements then i this is what i would add into the array

DefaultStyledDocument.ElementSpec[] esArray = {
        esStart1,
        esEnd1,
          esStart2,
          esEnd2};

The thing is i would not be able to know whatever the fixed array size is but this array has to be of an exact size because it is an element that is being added to the styled document

What i want to able to do is to find a way in which i can add these elements to the styled document even after its array size has been declared thus expanding the array size and adding values to it.

Some examples or sample codings would really be helpful

I hope someone can help me with this problem

Thank You

Yours Sincerely

Richard West

if you want to dynamic array, you need to use ArrayList, check the documentation for details....

There's really no other way than creating a new array each time.

String[] original = new String[4];
String[] temp = new String[words.length+1];

original = temp;

I'm sure a performance hit would come out of that.

If your inserting a lot, maybe a LinkedList would be better. Let us know what you come up with.

Hi everyone,

I used the ArrayList, it worked perfectly for my uses

Thank You

Richard West

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.