943,972 Members | Top Members by Rank

Ad:
  • Java Discussion Thread
  • Unsolved
  • Views: 1629
  • Java RSS
Jun 17th, 2005
0

Arrays

Expand Post »
Hi everyone,


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


This is what i am trying to do

Java Syntax (Toggle Plain Text)
  1.  
  2. SimpleAttributeSet start = new SimpleAttributeSet();
  3. start.addAttribute(AbstractDocument.ElementNameAttribute, "start");
  4.  
  5. SimpleAttributeSet end = new SimpleAttributeSet();
  6. end.addAttribute(AbstractDocument.ElementNameAttribute, "end");
  7.  
  8.  
  9. DefaultStyledDocument.ElementSpec esStart1 = new DefaultStyledDocument.ElementSpec(start, DefaultStyledDocument.ElementSpec.StartTagType);
  10. DefaultStyledDocument.ElementSpec esStart2 = new DefaultStyledDocument.ElementSpec(start, DefaultStyledDocument.ElementSpec.StartTagType);
  11. DefaultStyledDocument.ElementSpec esStart3 = new DefaultStyledDocument.ElementSpec(start, DefaultStyledDocument.ElementSpec.StartTagType);
  12.  
  13. DefaultStyledDocument.ElementSpec esEnd1 = new DefaultStyledDocument.ElementSpec(end, DefaultStyledDocument.ElementSpec.EndTagType);
  14. DefaultStyledDocument.ElementSpec esEnd2 = new DefaultStyledDocument.ElementSpec(end, DefaultStyledDocument.ElementSpec.EndTagType);
  15. DefaultStyledDocument.ElementSpec esEnd3 = new DefaultStyledDocument.ElementSpec(end, DefaultStyledDocument.ElementSpec.EndTagType);
  16.  
  17. DefaultStyledDocument.ElementSpec[] esArray = {
  18. esStart1,
  19. esEnd1,
  20. esStart2,
  21. esEnd2,
  22. esStart3,
  23. 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

Java Syntax (Toggle Plain Text)
  1. DefaultStyledDocument.ElementSpec[] esArray = {
  2. esStart1,
  3. esEnd1}


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

Java Syntax (Toggle Plain Text)
  1. DefaultStyledDocument.ElementSpec[] esArray = {
  2. esStart1,
  3. esEnd1,
  4. esStart2,
  5. 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
Similar Threads
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004
Jun 17th, 2005
0

Re: Arrays

if you want to dynamic array, you need to use ArrayList, check the documentation for details....
Reputation Points: 25
Solved Threads: 11
Junior Poster
tonakai is offline Offline
121 posts
since Feb 2005
Jun 17th, 2005
0

Re: Arrays

There's really no other way than creating a new array each time.
Java Syntax (Toggle Plain Text)
  1. String[] original = new String[4];
  2. String[] temp = new String[words.length+1];
  3.  
  4. original = temp;

I'm sure a performance hit would come out of that.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jun 20th, 2005
0

Re: Arrays

If your inserting a lot, maybe a LinkedList would be better. Let us know what you come up with.
Reputation Points: 113
Solved Threads: 19
Postaholic
server_crash is offline Offline
2,108 posts
since Jun 2004
Jun 21st, 2005
0

Re: Arrays

Hi everyone,

I used the ArrayList, it worked perfectly for my uses

Thank You

Richard West
Reputation Points: 25
Solved Threads: 10
Practically a Master Poster
freesoft_2000 is offline Offline
623 posts
since Jun 2004

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Java Forum Timeline: My class cannot be reuse
Next Thread in Java Forum Timeline: java 2d games problem (collision, well prevention)





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC