Is this a bug in Java, or am I doing something wrong?

I have a Vector which I want to convert to an array, so I wrote my code something like this:
MyObject a = new MyObject("A");
MyObject b = new MyObject("B");
MyObject c = new MyObject("C");
MyObject d = new MyObject("D");
Vector v = new Vector();
v.add(a);
v.add(b);
v.add(c);
v.add(d);
MyObject[] list = new MyObject[v.size()];
list = (MyObject[]) v.toArray();This should work, however it gives me a ClassCastException at runtime. Of course, I found ways around it, like using
v.toArray(list);But using the method this way does not make any sense because according to the API it is not void, but supposedly returns another array of Objects in addition to filling up the array which you pass to it.
Does anyone know the correct way to use the toArray() method, or it's even more mysterious overloaded companion, toArray(Object[] a)?

With regards to the Vector.toArray() method that takes an Object[] as an argument, you would normally use it this way:
Vector vector = <whatever>; // contains only instances of MyClass
MyClass[] array = (MyClass[])vector.toArray(new MyClass[vector.size]);While you're right that if you declare the array first and then simply pass it to the toArray() method that the array will get filled with the appropriate values. However, the array that is returned is not necessarily the array that was passed in. If the array that is passed in is shorter than the number of elements in the Vector, a new array of the same type as the one passed in is allocated and it is that array which is returned. The API only says that an array is returned, not the original one. Here is the source code for the toArray() method for you to look at:
    public synchronized Object[] toArray(Object a[]) {
        if (a.length < elementCount)
            a = (Object[])java.lang.reflect.Array.newInstance(
                                a.getClass().getComponentType(), elementCount);
     System.arraycopy(elementData, 0, a, 0, elementCount);
        if (a.length > elementCount)
            a[elementCount] = null;
        return a;
    }Shaun

Similar Messages

  • IndentAndSpacingFormat - Bug in API or am I doing something wrong?

    I have a FieldObject in my report with double line spacing (LineSpacing=2) and then try following code to see its spacing format:
    double lineSpacing = myFieldObject.FieldFormat.StringFormat.IndentAndSpacingFormat.LineSpacing;
    Unfortunately this returns 1 instead of 2! It always returns 1, no matter which field object I try it for. Am I doing something wrong?
    Thanks in advance...

    Hi John,
    I've duplicated the issue. It seems to be a problem only when the field type is defined as Memo in CR Designer. If the field is a String type then it returns the correct value.
    I'll have to escalate this to R&D to fix. It will take 2 or more months depending on their load or more.
    Verified indents do not return the correct value also. Likely related to the field type.
    Thank you
    Don
    Edited by: Don Williams on Jan 29, 2009 2:17 PM
    Edited by: Don Williams on Jan 29, 2009 2:46 PM

  • Is this a bug in Java!

    int i=0;
    i=i++;
    System.out.println(i);
    What should this code print.
    I guess 1.
    But it prints 0.
    Can anyone explain the logic behind this, or is this a bug in Java.
    Thanks for your time in advance.

    What did the mackerel do to deserve that?it wrote Sun's search function !!!!!! :pPlease, a mackerel would of done a better job.
    All I can say is "JTable project", smiles.
    (Gah, Please kill me)mlk BANG BANG*
    *Post decease operator.                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                               

  • Is this a bug in ipod software or am i doing something wrong?

    I got my ipod video a few weeks ago and was excited to be able to carry all 7000+ songs around in one place.. but then i ran into this problem and i'm not sure if it's a bug or if i'm doing something wrong.
    let's say i have exactly one album by an artist.. and i also happen to have some singles from the artist, with no album set... (i prefer not to have albums set for songs i don't have the entire album for, to keep from having to sort through a truckload of albums i don't completely have).
    when i browse by artist, and i select the artist, only the songs in the album shows up, all the non-album labeled songs are not visible! the same thing happens when i choose by genre..
    in fact the only way to find those songs not bound to an album is to select them from the song list.. and having to scroll through 7000+ songs to find one really bites..
    curiously enough, if there is two or more albums, then i can find the songs without albums.. definately sounds like a bug to me.. the software for the ipod video is only at v1.0, maybe they'll fix this (hopefully other ipods don't have this problem, i have no way of knowing though).
    one work around i have tried is to set an empty album for the artist's song that has none, but i mean i don't want to do that for all my non-albumed songs because i'm interested in preserving the timestamp of songs (to know when i got them) and i can't reasonably search for songs that have exactly one album to just set those..

    when i go to Music > Artist > All, as you suggested, i see all the albums i have listed. doesn't really relate to my problem with artists with songs in one album, as well as song w/o album set, and the latter not showing up.
    but that reminds me, why is it when i go to the All section from a particular artist and they do have more than one album (or even no albums), the order of the songs is in the order they are in the albums.. if i wanted the order it is on albums, i would have selected that album for example.. it would be nice if the songs were in alphabetical order when you're NOT browsing by album, making a particular song easier to find (what if you have thirteen albums for example and u aren't sure which album a song is on, now you have to scroll through a seemingly random ordered list to find one song, alphabetical would be SOOOO helpful!)

  • Bug in shuffle? or am I doing something wrong?

    Hi,
    I have a number of 1-minute clips that each have a 3 second text name on them in track v2. I've decided I want to re-order some of the clips, and I'm having more trouble with it than expected.
    First I tried to attach the name to the audio and video clips using
    Modify->Link, but that does not seem to work for linking two video clips in the timeline together. (I also tried attaching the text to one of the audio tracks, but all that did was unlink the "real" video that went with the audio.)
    I decided it's not that hard to just select both video tracks for each move, I'll just shuffle them together. I can't get shuffle to work properly! It worked (sortof) once. I selected both video and audio tracks, dragged them to the new location, hit "Option", watched the pointer change, and released the mouse. The selection moved, but instead of rippling the change down, it left a gap. It did not overwrite the other item in the track as though I had not hit option.
    I can work around that I'll just be left with a gap as I move segments forward to where the correct sequence is.
    Now comes the problem: I can't even repeat that! It seems no matter what I do, I can't shuffle segments in the timeline, they will overwrite. I've tried moving just the V1 segment, with the intent of moving the text later, no luck; it overwrites! I make sure that the arrow changes when I press the option key, and it does, although sometimes it changes to the right arrow instead of the little bendy one. Either way it overwrites instead of inserting. I've tried cmd-x cmd-v to cut and paste, but again it overwrites.
    Is this a bug I've triggered? or am I doing something wrong? I've read through page 200-201, volume 2 of the manual fairly carefully and I think I'm doing it right.
    I am using FCP 5.0.4.
    Thanks,
    --Beth
    PS. Even if it did work correctly, I think I'd prefer to shuffle automatically, and overwrite by pressing the option key. Is there any way to set that in a preference?

    It always works for me. You cannot shuffle multiple clips. Well, if you do, it will leave a gap. Why? dunno.
    I believe if your video and audio are not linked (cmd-L and you can only link audio and video, not multiple video clips) they will be considered multiple clips.
    The "bendy" arrow is the one you want and it varies based on where in the track your pointer is. Lower - insert, upper (I think) leaves a gap. Also, I always press the option key after I've started moving, but before it settles, but I don't think that should affect your operation as long as you push option after you've started moving, but before you let go.
    As to changing the option behavior, I don't think there's a way...
    Patrick

  • I have a Windows 8.1 and it's even running slow.  I'm a complete novice with computers (I've only had this one for 3 weeks) and I'm probably doing something wrong, but I haven't a clue what....

    I have a Windows 8.1 and it's even running slow.  I'm a complete novice with computers (I've only had this one for 3 weeks) and I'm probably doing something wrong, but I haven't a clue what.
    The tools are not responding when I try to use them.  Some of them work sometimes, but not others and some don't work at all.  I'm in a design class online and I need these tools desperately.  I have an assignment due Monday and I'm losing the whole weekend because Tech Support is only open M-F!
    Any help anyone can give me will be appreciated.
    Thanks,
    Rose Ireland

    Maybe these links provide some pertinent information.
    Optimize performance | Photoshop CS4, CS5, CS6, CC
    Photoshop: Basic Troubleshooting steps to fix most issues

  • Rented a movie and it take 60 minutes to download, will this ever be fix or am I doing something wrong?

    Every time I rent a movie on my new apple tv it always take 1 hour to down load, am I doing something wrong? can this issue be fixed?

    You need a faster internet connection. After I upgraded to a 6 meg connection that probelm went away.
    How fast is your internet connection?

  • HT1766 My iCloud back up estimates a waiting time of 21 hours, is this accurate or am I doing something wrong?

    I wonder if im doing something wrong. The phone is plugged into the power outlet and occasionally I check to see if it progresses. I am doing it manually thru the icloud section.

    >When you use the includeIn command, by default the object will only be created when it is needed. Hence, you will execute a creationComplete
    >event, and therefore atleast 100 million (sometimes 400 million) loops, when each required Button state is needed. Obviously, once each button
    >state has been created the creationComplete handler (invalidateDisplayList) will not usually be called again.
    So are you saying there is a better way or another way to do that? Or are you just telling me that's how it works? Because I know that the includeIn isn't creating the other buttons untill they are needed. I need them created when the button is created. That is my question, how do I get the other states to be created when the button is created so that I don't see the button flash black?
    >As written, bioStyleChanged will always remain true
    >if(!bioStyleChanged) {
    >       bioStyleChanged = false;
    >}    
    >this code does nothing for you.
    Sorry, I missed that when cleaning up the code to ask for help.
    Thanks,
    cdman52

  • HT2404 i am trying to update my operating software for a iphone 3g to enable me to download io5 and icloud....can I actually do this as i've tried in vain and am getting no where..added to this I am a relative novice so Im probably doing something wrong.

    i am trying to update my operating software for an iphone 3g to enable me to download io5 and icloud....can I actually do this as i've tried in vain and am getting no where..added to this I am a relative novice so Im probably doing something wrong.

    Do you have an iPhone 3G or 3 GS? The 3G can not be upgraded beyond 4.2.1
    Stedman

  • RMBP: Sometimes, after having been shut down, i have to press the power button several times before the machine starts up. It's 3 weeks old, and this has happened 2 times now. Is there something wrong with my rMBP?

    rMBP: Sometimes, after having been shut down, i have to press the power button several times before the machine starts up. It's 3 weeks old, and this has happened 2 times now. Is there something wrong with my rMBP?

    Did you purchase the Mac new?
    You have 14 calendar days from the time your items are delivered to initiate a return as noted here.
    Even though you are past the 14 day limit, if it were me, I'd take the Mac back and explain what's going on. Your Mac should work out of the box.
    message edited by:  cs

  • Trying to download Lion trought App store, it downloads and it asks for account login and it installs again it never shows installed it always shows installing. this is the third time it's installing. Am I doing something wrong?

    Been trying to download Lion from the App Store.  It saks for login, I login in and it starts install.  After about 13 hours it finishes download then asks for login again.  After login, it starts installing again. It never shows installed.  This is the third time I am doing this.  Am I doing something wrong?

    Sorry, my bad, we have the iMac G5 10.6.8.  I just got home and the message said an error occurred and was unable to download.  Am I doing something wrong or is there something I need to do.

  • When ever I go into my itunes I get this registered user message for Sprint.  Am I doing something wrong?

    Whenever I go into my Itunes I get this Registered User message for Sprint.  Am I doing something wrong?

    What is the exact message?
    Sprint has nothing to do with iTunes or iPads.

  • Is this a bug in APEX or I'm missing something

    Hi,
    I have a table chr10bug( id number , sample_text varchar2(4000)).
    I am using simple form with textarea to populate sample_text.
    I have a report based on above table.
    The report query is select id , sample_text from chr10bug. The Link tab has Target as URL. URL is javascript:void(0); and Link attribute has a very simple function
    onmouseover="alter('#SAMPLE_TEXT#')"
    Now, my question is when a user hits enter in the textarea eg.
    User enter some text **User Press Enter**
    User enter some text again
    In the reports the onmouseover="alert('#SAMPLE_TEXT#')" does not work for recrod where users pressed enter key. (In fact none of the function which accept string is working) . If we do not press enter while entering data in textarea... the above function is working...
    Example is here....
    http://apex.oracle.com/pls/apex/f?p=32555:1 ( report)
    http://apex.oracle.com/pls/apex/f?p=32555:2 ( form to enter data via text area)
    Is this a bug...
    Regards,
    Shijesh
    Edited by: Apex_Noob on Nov 18, 2010 12:18 PM

    Hi,
    Thanks once again for reply.
    I create two report with same javascript function alert('#sample_text#')
    CASE1 >> Javascript function call is placed in Link attribute section as ** onmouseover="alert('#sample_text#')" >>> This is not working
    CASE2 >> Javascript function call is place in URL section as ** javascript:alert('#sample_text#') >>>> This is working however I'm not able to have onmouseover feature.
    Any alternative to have onmouseover=myFunction('#COLUMN_NAME#').
    http://apex.oracle.com/pls/apex/f?p=32555:1:8742069995710311:::::
    thanks,
    Shijesh
    Edited by: Apex_Noob on Nov 18, 2010 12:45 PM

  • Is this a bug or am I doing something wrong? (Custom Buttons)

    I have a project where I've created a custom buttonbase which has a custom skin.
    Create a button that uses the custom skin which extends the custom button base the first time the button is used it doesn't work the way I would expect.
    Here's what happens. The first time the app is loaded the button's up state shows up just fine. If I mouse over the button it flashes black for a second while it loads the mouse over state. If I mouse out and over again it works normally. Now if I click on the button it flashes black again while it loads the selected state and then the selected over state. After the first time the button functions correctly. But naturally I don't want the button to flash black the first time it's used. I want it to show my custom skin the first time not the second and subsequent times.
    I've included a project that demonstrates the problem. I have added some for loops in the part of the code that determines the custom colors for the button, just so that the black flash is really obvious.
    If you look at it there are three classes. The ButtonSkinBase which defines the base look for all skinned buttons. The ButtonSkin which is a specific skin to use. And the ButtonGraphic which is just a custom definition for a button.
    I think the problem is in the SDK in that it isn't loading the ButtonGraphics in the ButtonSkinBase class before they are used. It loads them when they are used. However, I'm not that experienced with the way the SDK functions under the covers so it could be that I'm not doing something I need to do.
    Any help with this would be great.
    Thanks,
    cdman

    >When you use the includeIn command, by default the object will only be created when it is needed. Hence, you will execute a creationComplete
    >event, and therefore atleast 100 million (sometimes 400 million) loops, when each required Button state is needed. Obviously, once each button
    >state has been created the creationComplete handler (invalidateDisplayList) will not usually be called again.
    So are you saying there is a better way or another way to do that? Or are you just telling me that's how it works? Because I know that the includeIn isn't creating the other buttons untill they are needed. I need them created when the button is created. That is my question, how do I get the other states to be created when the button is created so that I don't see the button flash black?
    >As written, bioStyleChanged will always remain true
    >if(!bioStyleChanged) {
    >       bioStyleChanged = false;
    >}    
    >this code does nothing for you.
    Sorry, I missed that when cleaning up the code to ask for help.
    Thanks,
    cdman52

  • HT1751 I am trying to backup my iTunes library onto an external hard drive but it won't complete due to 'insufficient privileges for some of the items'. Am I doing something wrong and is there a solution to this problem?

    Hi All,
    I am currently trying to backup my iTunes library onto an external hard drive so that I can copy it onto my new Macbook Pro. I have been following the steps posted on the Apple website however each time I get a dialogue box stating:
    'The operation cannot be completed because you do not have sufficient privileges for some of the items'.
    Is this because I have items that were bought under another persons iTunes account? If so, is there a solution to this problem? Or am I doing something completely wrong?
    Thanks in advance,
    Sbrooks19

    Go through the below forum with similar issue and follow the process mentioned by "Macjack" in the beginning.
    https://discussions.apple.com/thread/4499820

Maybe you are looking for

  • 10G RAC on AIX without ASM & HACMP.

    Hi, I want to install ORACLE 10G RAC on AIX but I dont want ot use ASM or HACMP/GPFC. Just want to install with CRS and RAW devices.Is it possible to install 10g RAC with CRS and RAW devices. What is the best approach for this. Regards MMU

  • Can I download Pages for free onto my late 2012 macbook pro

    Can I download Pages for free onto my late 2012 Macbook Pro with retina display and 8 gb of memory.  The machine is running Yosemite Versus 10.10.1. 

  • Nokia 5530 - Applications wont start

    I sent some games to my Nokia 5530 via bluetooth, installed successfully and working, so then i made a folder in Applications directory and moved all the games onto that folder. The next day i turn on my phone, start a game it wont load up, none of t

  • Alter system archive log current

    Hi everyone, I write code to alter system archive log current in PL/SQL as following: declare start_sequence number; end_sequence number; begin --- get start sequence# --- select SEQUENCE# into start_sequence from v_$log where status='CURRENT'; --- g

  • Itunes won't install on my computer

    Itunes won't download on my comnputer. It says errors ocuured during installation. Your system has not been modified.