? with a Vector of LinkedLists

Hi,
I would like to create a Vector. Each element will be a LinkedList. Each element in my LinkedList will be an Object, call it o. Obejcts like o have a member field called time, which is a BigDecimal. I am doing this because I want each LinkedList to contain o's with the same time value. So if I index into the Vector at 0 I will get a linked list that has Objects of type o and all those objects have time value t0. If I index into the Vector at position x, I will get a LinkedList of objects having time value tx.
Now, initially my Vector just contained the Objects of type o, so my class just had a method called compareTo() that would provide a natural ordering. This allowed me to do a Collections.binarySearch() when I wanted to insert into the vector.
Currently I am a little confused as to how and where I will write the new compareTo() method. Basically I have a method that accepts objects of type o and what I want it to do would be to look at o's time field, then do Collections.binarySearch() on the Vector to find the index of the appropriate LinkedList. Then I will just insert into the LinkedList. But I see that the LinkedList class does not have it's own compareTo() method. If I were to write one I would have it simply call the compareTo() method on the first element in it's list.
Thanks,
Jeff

Oh, ok. So you are basically implying that since my
times will be arbitrary then my binarySearch into the
Vector will cost more than just inserting a new
LinkedList into a HashMap (or getting the already
existing linked list from the HashMap). Makes sense.
Thanks.Not necessarily that it will be computationally more expensive, just that the vector is a poorer representation of your intent. A Vector (actually, ArrayList would be better) or Array is useful for random access when you know the index. If you want a lookup by an arbitrary key, however, a Map is a more direct realization of your design.
Under the covers, the Map could very well do a binary search on a List or array anyway, but your code shouldn't know or care about that detail. Your code should just say, as simply and directly as possible, "Give me the elemnts that have a time value of X". map.get(X) is a clean way to do that. Anybody reading the code later (including you, after you've forgotten WTF you were doing) will find that easier to understand than a binary search through a vector to find the list element whose first entry have time value X.

Similar Messages

  • Strange Problem with a Vector wraped inside a Hashtable

    Hi all ,
    I'm having a strange problem with a Vector wraped within a Hashtable inherited Class.
    My goal is to keep the order of the elements of the Hashtable so what I did was to extend Hashtable and wrap a Vector Inside of it.
    Here is what it looks like :
    package somepackage.util;
    import java.util.Enumeration;
    import java.util.Hashtable;
    import java.util.Vector;
    public class OrderedHashTable extends Hashtable {
        private Vector index;
        /** Creates a new instance of OrderedHashTable */
        public OrderedHashTable() {      
            this.index = new Vector();
    //adds a key pair value to the HashTable and adds the key at the end of the index Vector
       public synchronized Object put(Object key,Object value){      
           index.addElement(key);
           Object obj = super.put(key,value);
           System.out.println("inside OrderedHashTable Put method index size = " + index.size());
           return obj;    
    public synchronized Object remove(Object key){
           int indx = index.indexOf(key);
           index.removeElementAt(indx);
           Object obj = super.remove(key);
           return obj;
    public synchronized Enumeration getOrderedEnumeration(){
           return index.elements();
    public synchronized Object getByIndex(int indexValue){
           Object obj1 = index.elementAt(indexValue);
           Object obj2 = super.get(obj1);      
           return obj2;
       public synchronized int indexOf(Object key){
        return index.indexOf(key);
        public synchronized int getIndexSize() {
            return index.size();
        }Everything seemed to work fine util I tried to add objects using a "for" loop such as this one :
    private synchronized void testOrderedHashTable(){
            OrderedHashTable test = new OrderedHashTable();
            for (int i = 1 ; i<15; i++){
                 System.out.println("adding Object No " + i);
                 String s = new String("string number = "+i);
                 test.put(new Integer(i),s);
                 System.out.println("-----------------------------------");
            //try to list the objects
            Enumeration e = test.getOrderedEnumeration();
            while (e.hasMoreElements()){
                Integer intObj = (Integer) e.nextElement();
                System.out.println("nextObject Number = "+ intObj);
        }Here is the console output :
    Generic/JSR179: adding Object No 1
    Generic/JSR179: inside OrderedHashTable Put method index size = 1
    Generic/JSR179: -----------------------------------
    Generic/JSR179: adding Object No 2
    Generic/JSR179: inside OrderedHashTable Put method index size = 2
    Generic/JSR179: -----------------------------------
    Generic/JSR179: adding Object No 3
    Generic/JSR179: inside OrderedHashTable Put method index size = 3
    Generic/JSR179: -----------------------------------
    Generic/JSR179: adding Object No 4
    Generic/JSR179: inside OrderedHashTable Put method index size = 4
    Generic/JSR179: -----------------------------------
    Generic/JSR179: adding Object No 5
    Generic/JSR179: inside OrderedHashTable Put method index size = 5
    Generic/JSR179: -----------------------------------
    Generic/JSR179: adding Object No 6
    Generic/JSR179: inside OrderedHashTable Put method index size = 6
    Generic/JSR179: -----------------------------------
    Generic/JSR179: adding Object No 7
    Generic/JSR179: inside OrderedHashTable Put method index size = 7
    Generic/JSR179: -----------------------------------
    Generic/JSR179: adding Object No 8
    Generic/JSR179: inside OrderedHashTable Put method index size = 8
    Generic/JSR179: -----------------------------------
    Generic/JSR179: adding Object No 9
    Generic/JSR179: inside OrderedHashTable Put method index size = 10
    Generic/JSR179: inside OrderedHashTable Put method index size = 10
    Generic/JSR179: -----------------------------------
    Generic/JSR179: adding Object No 10
    Generic/JSR179: inside OrderedHashTable Put method index size = 11
    Generic/JSR179: -----------------------------------
    Generic/JSR179: adding Object No 11
    Generic/JSR179: inside OrderedHashTable Put method index size = 12
    Generic/JSR179: -----------------------------------
    Generic/JSR179: adding Object No 12
    Generic/JSR179: inside OrderedHashTable Put method index size = 13
    Generic/JSR179: -----------------------------------
    Generic/JSR179: adding Object No 13
    Generic/JSR179: inside OrderedHashTable Put method index size = 14
    Generic/JSR179: -----------------------------------
    Generic/JSR179: adding Object No 14
    Generic/JSR179: inside OrderedHashTable Put method index size = 15
    Generic/JSR179: -----------------------------------
    Generic/JSR179: nextObject Number = 1
    Generic/JSR179: nextObject Number = 2
    Generic/JSR179: nextObject Number = 3
    Generic/JSR179: nextObject Number = 4
    Generic/JSR179: nextObject Number = 5
    Generic/JSR179: nextObject Number = 6
    Generic/JSR179: nextObject Number = 7
    Generic/JSR179: nextObject Number = 8
    Generic/JSR179: nextObject Number = 9
    Generic/JSR179: nextObject Number = 9
    Generic/JSR179: nextObject Number = 10
    Generic/JSR179: nextObject Number = 11
    Generic/JSR179: nextObject Number = 12
    Generic/JSR179: nextObject Number = 13
    Generic/JSR179: nextObject Number = 14
    You can notice that the output seems correct until the insertion of object 9.
    At this point the vector size should be 9 and the output says it is 10 elements long ...
    In the final check you can notice the 9 was inserted twice ...
    I think the problem has something to do with the automatic resizing of the vector but I'm not really sure. Mybe the resizing is done in a separate thread and the new insertion occurs before the vector is resized ... this is my best guess ...
    I also tested this in a pure J2SE evironment and I don't have the same strange behavior
    Can anybody tell me what I am doing wrong or how I could avoid this problem ?
    Thanks a lot !
    Cheers Alex

    Am i doing anything wrong?Uhm, yes. Read the API doc for addElement() and for addAll()

  • How can we serialize a c++ structure with an vector inside it in coherence using PofWriter?

    How can we serialize a c++ structure with an vector inside it in coherence using PofWriter?
    Any help is appreciated.
    Thanks.

    The link you gave was the same link that had already been given, and that points to an article about AIR 3.6.
    I’m not sure if AIR 3.9 has changed fundamentally with regard to this problem though, but here’s a work around someone used, to make the subsequent loads seem like a new file:
    https://gist.github.com/sportebois/6969008
    Would it be possible to just keep a reference to the loaded swf, after the first load? Then you can removechild it when you’re not needing it, and addchild it back when you do need it.

  • Speed up Illustrator CC when working with large vector files

    Raster (mainly) files up to 350 Mb. run fast in Illustrator CC, while vector files of 10 Mb. are a pain in the *blieb* (eg. zooming & panning). When reading the file it seems to freeze around 95 % for a few minutes. Memory usage goes up to 6 Gb. Processor usage 30 - 50 %.
    Are there ways to speed things up while working with large vector files in Illustrator CC?
    System:
    64 bit Windows 7 enterprise
    Memory: 16 Gb
    Processor: Intel Xeon 3,7 GHz (8 threads)
    Graphics: nVidia Geforce K4000

    Files with large amounts vector points will put a strain on the fastest of computers. But any type of speed increase we can get you can save you lots of time.
    Delete any unwanted stray points using  Select >> Object >> stray points
    Optimize performance | Windows
    Did you draw this yourself, is the file as clean as can be? Are there any repeated paths underneath your art which do not need to be there from live tracing or stock art sites?
    Check the control panel >> programs and features and sort by installed recently and uninstall anything suspicious.
    Sorry there will be no short or single answer to this, as per the previous poster using layers effectively, and working in outline mode when possible might the best you can do.

  • [svn] 1317: Updates to get flex working with the Vector stuff.

    Revision: 1317
    Author: [email protected]
    Date: 2008-04-20 12:12:54 -0700 (Sun, 20 Apr 2008)
    Log Message:
    Updates to get flex working with the Vector stuff. Had to add support for the new name type, and new applytype opcode to the Optimizer/Merger that flex uses. Also had to slightly change how the instantiated vector classes find their declared methods. Previously they were copied into the new class, but now they just point to the original vector class.
    Modified Paths:
    flex/sdk/trunk/modules/asc/src/java/macromedia/abc/ConstantPool.java
    flex/sdk/trunk/modules/asc/src/java/macromedia/abc/Decoder.java
    flex/sdk/trunk/modules/asc/src/java/macromedia/abc/Encoder.java
    flex/sdk/trunk/modules/asc/src/java/macromedia/abc/OpcodeVisitor.java
    flex/sdk/trunk/modules/asc/src/java/macromedia/abc/Opcodes.java
    flex/sdk/trunk/modules/asc/src/java/macromedia/abc/Printer.java
    flex/sdk/trunk/modules/asc/src/java/macromedia/abc/Scanner.java
    flex/sdk/trunk/modules/asc/src/java/macromedia/abc/Visitor.java
    flex/sdk/trunk/modules/asc/src/java/macromedia/asc/parser/Parser.java
    flex/sdk/trunk/modules/asc/src/java/macromedia/asc/semantics/ConstantEvaluator.java
    flex/sdk/trunk/modules/asc/src/java/macromedia/asc/semantics/FlowAnalyzer.java
    flex/sdk/trunk/modules/asc/src/java/macromedia/asc/semantics/ReferenceValue.java

    despite the workaround, it doesn't fix the real problem. It shouldn't be a huge deal for adobe to add support for multiple svn versions. Dreamweaver is the first tool i've used that works with svn that doesn't support several types of svn meta data. If they're going to claim that Dreamweaver supports svn is should actually support svn, the current version, not a version several years old. This should have been among the first patches released, or at least after snow leopard came out (and packaged with it the current version of svn).
    does anyone know if the code that handles meta data formatting is something that is human readable, or where it might be, or is it in compiled code.
    i signed up for the forums, for the sole purpose of being able to vent about this very frustrating and disappointing situation.

  • Help with a Vector Array - pppppplllllllleeeeaaasse!!!!!!!

    Hi I'm having a mare with a Vector Array that I want to contain String variables. I know you get a warning with the traditional implementation of a vector, and I have over come that using:
    private Vector <String> MyVector = new Vector<String>();
    But I also need to create a Vector Array and I am getting some very strange results - for a start in JPadPro it only comiles every second attempt, with every other failing due to the implementation of the Vector array.
    Below is a snippet of the code that I have sort of got to compile but with a compiler warning:
    private Vector<String>[] MyVector; //this is line 75
    int Array;
    public myClass()
    Array = 5;
    MyVector = new Vector[ Array ];
    for( int i = 0; i < Array; i++ )
    MyVector[ i ] = new Vector<String>();     
    The first time I complie I get:
    CreateMessage.java Line 75: Syntax Error
    The Second time I compile I get:
    CreateMessage.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    Finished
    I hate complier errors - I have gotten quite a few but have managed to remove them all but this one by searching the forum. Any help on the correct implementation of my Vector Array would be great,
    Thanks in advance,
    Kris

    Mr_Bump180 wrote:
    Well you lot are a real cheery bunchDo you come here for warm and fuzzies or for Java help?
    - please accept my most humble apologies for daring to try and keep my thread light hearted. Use your brain. Nobody objects to keeping your thread lighthearted. It's that garbage like that is annoying and makes your post harder to read. If you want to get your question answered clear communication is of the essence. That should be common sense.
    I asked the question simply as I wanted to improve my code - which I write for no gain, but just in an attempt to automate processes that I cannot find software to do for me. How is that relevant?
    I find the solutions on this website invaluable, but what shines through most clearly - is the incredibly poor attitude of some of you - who are obviously java professionals - when a novice risks posting a question on what is obviously YOUR forum. Is that how you get your kicks? By jumping all over people that dare to ask a question in order to try and better themselves?Right. People jumped on your for asking a question. Get real.
    I will as I always do, read the pages you have linked to, and I will also try and implement your solution into my code - I just find it a shame that in order to obtain any help people who are posting in the "New to Java Programming" by-the-way, run the gauntlet of your abuse if the question, grammar, syntax or perhaps even title don't measure up to your high standards.Dude, if you're asking somebody to volunteer his time to help you, isn't it obvious that it's in your best interest to make it as easy as possible for him to do so?
    I will now turn the watch off this thread, and will not reply againAs if anybody cares.
    - but for those of you reading - I wonder just how many can't bare me to have the last word and have to post a replySo, anytime somebody replies to something it must be because they have to have the last word?
    with the clever little quote inserts, a greater than thou attitude, and some more brilliant humour (I was in stiches reading your rebuttals ) because I don't fully understand a concept and as such asked for help.Get over the "they teased me because I don't know Java" bullshit. It's simply not the case, and you bloody well know it.

  • Working with big vectors

    My program works with a large vector of objects.
    The problem is that the vector is to big to be stored in memory.
    Is there any utility that will store the whole vector in a file
    and seamlessly cache parts of it to memory ?
    Thanks.

    It seems I will have to build my own file mapping.
    Since all my objects have the same size (e.g. all
    Integers or String),
    what is the best and fast way to read a specific
    object (e.g. vector[200]) ? Also, what is the fast way
    to read a bulk of objects (e.g. vector[200-300]) ?If your are dealing with integers or Strings I don't think you should go for a Vector filled with objects solution. You can minimize the memory requirements quite a bit if you use arrays.
    Integers. Why not use an int array? Each int takes up 4 bytes in memory but an Integer Object adds an overhead of maybe 16 byte. If you use int's your memory need shrinks to a fifth.
    Strings. A char takes 2 bytes. Maybe you can store a String as a byte array instead, thus saving 1 byte per char. The memory need shrinks to a half. It would be even more efficient if you just dumped all strings in one large byte array (with a null byte terminating each string). In addition you would need a second array with the start positions to the strings.
    These are the densest in memory data structures you can get. If you still have too much data you will have to go for files. The in memory solutions I've suggested can be directly used with files (just substitute array with file).

  • Working with labelled vector graphics (CS4)

    I've working with a rather large set of simple geometric vectors.
    They've all been created in illustrator, but need to be scaled in InDesign and flow with the text.
    The only method I've come up with is somewaht convoluted, so I thought I'd share and see if anyone had any suggestions on how I might improve the workflow.
    - vector graphic is pasted (not placed) into InDesign and ungrouped
    - strokes are converted (manually) to object styles (graphic styles created in illustrator are lost)
    - graphis is regrouped and roughly scaled
    - labels are added and paragraph styles applied
    - labels are grouped with graphic, cut, and "paste into" anched text box
    - re-scaling and label edits are done with select content/container/next object
    More complicated graphics will be linked and rescaled in illustrator and relabelled and placed as above.
    Is there a better/easier way to do this?
    (Maybe CS5 will have a container isolation mode...)

    I want to keep the labels out of illustrator, but there are a few graphics with more detail that percentage scaling is the way to go.
    It would be far easier if InDesign would respect (i.e. import, map) Illustrator's graphic styles.

  • Exporting a composition with Ai vectors

    What is the best format to export a composition containing Illustrator vector layers? I used to use Quicktime, but this setting doesn't seem to work as well anymore in CC.

    Hey there!
    I'm with Dave on this one, don't expect miracles out of PAL DV. However, if your on Mac, I would suggest Quicktime and Apple ProRes 422 codec. I usually use this to get good results out of a render.
    You can also try Adobe Media Encoder and go for a h.264 with a good bitrate.
    Now, these facts are not backed by any hard evidence with numbers haha, it's just what I usually go for and it works fine
    Good luck!

  • When I draw with the vector pen and save the path, if I open it in illustrator does it open properly?

    Hi
    This is a newbie sort of question, so thank you for your patience. I barely use vector images. But I need to draw an entire graphic in vector so I have drawn it using the pen.
    My question is if I save the path (.ai) file when I open it in Illustrator will it export accurately?

    If you save your file from Photoshop as a .PSD, and open it in Illustrator with Convert Layers to Objects selected, the paths will be there, along with the Fill and Stroke.

  • Plz help with my vector... :/ :)

    hi...
    i've seen a lot of answers about reducing or increasing the size of the
    vector because the for-loop may ask for objects in the index of the vector that may not exist, but i believe that this is not the case in the specific problem because the x<=planes.size() gives exaclty the scope that the for-loop will look for.
    i havent referenced all the code but the parts that i believe that they are crucial....
    also i've tried to initialize the vector with a lot of values but it didnt helped me....
    any comments would be thankfull... :)
    protected Vector planes;
    protected Plane currentPlane;
    planes = new Vector();
    public void getArrivals() {
          for (x=0; x<=planes.size(); x++)
              currentPlane = (Plane)planes.elementAt(x);
              if ( currentPlane.getStatus() == "Entered the Airport Area" ){
                 System.out.println ("The flight " + currentPlane.getFlightNumber() +
          " to " + currentPlane.getCity() + " has just " + currentPlane.getStatus());
          }Exception in thread "main" java.lang.ArrayIndexOutOfBoundsException: 5 >= 5
    at java.util.Vector.elementAt(Vector.java:427)
    at ListOfPlanes.getArrivals(ListOfPlanes.java:94)

    Ain't life great? You get a bunch of answers in less than 15 minutes. :)
    Iterators eliminate the need to remember indices and array locations. Consequently you need to use a while loop instead of a for loop.
    It's useful in some situations but not all. If you want to use it, you code would look like:
    import java.util.Enumeration;
    public void getArrivals() {
             Enumeration enu = planes.elements();
          while (enu.hasMoreElements())
              currentPlane = (Plane)enu.nextElement();
              if ( currentPlane.getStatus() == "Entered the Airport Area" ){
                 System.out.println(// blah blah blah...
          }Enumeration is a variation of Iterator in that Enumeration is read-only while Iterator allows objects to be optionally removed one at a time.

  • Problem with a Vector of objects.

    I have a vectore that contains GItems, it needs to be traversed everytime paintComponent() is called.
    GItem stores the start point, end point and drawing mode (line, rect, circle, etc).
    These GItems are added to the Vector on MouseReleased using items.addElement(new GItem(sPoint, ePoint, mode))
    However, when they are called by paintComponent, they all have the same value as the last line drawn ????
    Heres my paintComponent, maybe you could spot my bug?
    public void paintComponent(Graphics g)  {
         GItem gi;
         Point p1, p2;
         super.paintComponent(g);
         for (int i=0;i<items.size();i++)  {  //items is my Vector
         gi = (GItem)items.elementAt(i);
         p1 = gi.start;
         p2 = g1.end;
         g.drawLine(p1.x, p1.y, p2.x, p2.y)
         g.drawline(sPoint.x, sPoint.y, ePoint.x, ePoint.y);
    [\code]                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                           

    Thats a good point //excuse the pun
    i'll try setting the mode values to see if new GItems are being created,
    if they are, then the point values must all be referencing the same object
    in which case ePoint.clone() should solve the problem
    Thanks

  • NEW! Free Dreamweaver templates WITH Fireworks vector files

    I just released some free Dreamweaver templates that include
    not only the Dreamweaver template dwt file, but also the original
    pre-sliced Fireworks file with all the template graphics.
    The templates include the dwt, Fireworks file, pure CSS
    menus, are AdSense-ready, XHTML 1.0 valid and are very customizable
    through both the Fireworks file and the CSS.
    If you use one, let me know so I can get a screenshot and
    post a link to your site showing how the templates have been
    customized by various designers.
    Cheers!
    Brodit

    Can you post a link to the PHP page, please? This will allow
    me to see the
    HTML markup on it....
    Murray --- ICQ 71997575
    Adobe Community Expert
    (If you *MUST* email me, don't LAUGH when you do so!)
    ==================
    http://www.dreamweavermx-templates.com
    - Template Triage!
    http://www.projectseven.com/go
    - DW FAQs, Tutorials & Resources
    http://www.dwfaq.com - DW FAQs,
    Tutorials & Resources
    http://www.macromedia.com/support/search/
    - Macromedia (MM) Technotes
    ==================
    "Mr Zedd" <[email protected]> wrote in
    message
    news:eviqn8$ntm$[email protected]..
    > Hello everyone,
    >
    > I have been given an exisiting website to update that
    uses Dreamweaver
    > templates.
    >
    > I have made the changes successfully within the
    content.dwt file, saved
    > it,
    > and then uploaded both the new content.dwt file and the
    related *.htm
    > pages
    > that were linked to it.
    >
    > After going to the website to double check the new pages
    I found a *.php
    > file
    > that uses the same template, content.dwt, but it never
    asked to update
    > that
    > particular file.
    >
    > So, I opened the *.php file in Dreamweaver and tried to
    apply changes from
    > the
    > content.dwt file to it using: Templates - Update current
    page, but no such
    > luck. Since there is no code at the start of this file
    like the other
    > *.htm
    > files how do I "connect" it with the newly updated
    template. I tried
    > making a
    > small change on the content.dwt file so see what files
    would come up in
    > the "do
    > you wish to update..." list but for some reason the
    *.php file is not
    > there.
    >
    > Perhaps the answer is fairly obvious to someone who has
    worked with
    > Templates
    > a lot but since I have just started working with
    templates I still have a
    > large
    > knowledge gap in this area.
    >
    > Any help would be appreciated.
    >
    > Many thanks
    >
    > Mr Zedd
    >

  • Problem with a vector

    I have this vector, and inside a vector, there are Strings[]
    So, i declared the vector and string like this:
    Vector allCodes = new Vector();
    String code[] = new String[5];Then i assigned tome values to code like this:
    code[0]="whatever";
    code[1]="whatever";
    code[2]="whatever";
    code[3]="whatever";
    code[4]="whatever";Then, the ihing is that, when i do this:
    allCodes.addElement(code);The first value gets overwritten, instead of adding it to a next entry in the vector........
    Am i doing anything wrong?
    Thanks
    Edited by: J.A. on Apr 19, 2010 6:36 AM

    Am i doing anything wrong?Uhm, yes. Read the API doc for addElement() and for addAll()

  • JSF with java vector object

    Hi;
    Would like to know if in JSF, it is allowing to program normal java command, like using vector, to add item and print out the value?
    I found out, many features available in JSP, which is not able to perform in JSF. Like, how to chk & print the size of list / collection object in JSF?
    Thanks in advance.

    Simply deploy the EAR in the application server.
    Or if you're using an IDE, import it as new EAR project, add it to the server and run it.

Maybe you are looking for