954,518 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Arrays

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

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

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

tonakai
Junior Poster
121 posts since Feb 2005
Reputation Points: 25
Solved Threads: 11
 

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.

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

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

server_crash
Postaholic
2,111 posts since Jun 2004
Reputation Points: 113
Solved Threads: 20
 

Hi everyone,

I used the ArrayList, it worked perfectly for my uses

Thank You

Richard West

freesoft_2000
Practically a Master Poster
623 posts since Jun 2004
Reputation Points: 25
Solved Threads: 10
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You