CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2

Thread: Arrays

  1. #1
    Join Date
    Aug 2004
    Posts
    147

    Question Arrays

    Hi everyone,


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


    This is what i am trying to do

    Code:
    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

    Code:
    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

    Code:
    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

  2. #2
    Join Date
    Feb 2004
    Location
    USA - Florida
    Posts
    729

    Re: Arrays

    How about using something like a List, and calling toArray() when needed? A List container, like an ArrayList, will take care of all the internal array management.
    Code:
    List lst = new ArrayList();
    lst.add(esStart1);
    lst.add(esEnd1);
    
    // and if need be add more...
    lst.add(esStart2);
    lst.add(esEnd2);
    ...
    
    // ok, need an array now, not an ArrayList...
    DefaultStyledDocument.ElementSpec[] esArray = (DefaultStyledDocument.ElementSpec[])lst.toArray(new DefaultStyledDocument.ElementSpec[lst.size()]);
    the toArray() line can be rewritten to be less convoulted:
    Code:
    DefaultStyledDocument.ElementSpec[] esArray = new DefaultStyledDocument.ElementSpec[] esArray[lst.size()];
    lst.toArray(esArray);
    If you insist on taking the DIY route for whatever reason, then you basically have no choice but to create a new instance of an array larger then the old one, copy the elements from the old array to the new array, and re-assign the old array reference to point to the new, larger array.
    Code:
    DefaultStyledDocument.ElementSpec[] temp = new DefaultStyledDocument.ElementSpec[esArray.length * 2];
    
    System.arraycopy(esArray, 0, temp, 0, esArray.length);
    
    esArray = temp;
    There's a small variation of this, but you should get the idea on which option is right for you.
    Hungarian notation, reinterpreted? http://www.joelonsoftware.com/articles/Wrong.html

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured