Quick generics problem

I understand how generics can be used to put control over the datatype to be stored but I'm not sure about
the distinct differences in the use of the following. could someone just explain the difference between each of them.
the first one has String specified in the declaration and creation of the vector, second one has <String> only specifed
in the decarlation of the vector, and the last only has <String> in the creation of the object.
Vector<String> vect = new Vector<String>();
Vector<String> vect1 = new Vector();
Vector vect2 = new Vector<String>();Thanks,
newbie

saru88 wrote:
I understand how generics can be used to put control over the datatype to be stored but I'm not sure about
the distinct differences in the use of the following. could someone just explain the difference between each of them.
the first one has String specified in the declaration and creation of the vector, second one has <String> only specifed
in the decarlation of the vector, and the last only has <String> in the creation of the object.
Vector<String> vect = new Vector<String>();
Vector<String> vect1 = new Vector();
Vector vect2 = new Vector<String>();
The second and third definitions will give you compiler warnings. I suggest you add
vect.add("1");
vect1.add("1");
vect2.add("1");
vect.add(1);
vect1.add(1);
vect2.add(1);to your test code and see what you get; it might help explain some things. Come back if you're still confused.
Winston

Similar Messages

  • Generics problem - unchecked and unsafe problems

    basically my teacher said it needs to run if she types in "% javac *.java " otherwise i'll get a zero...
    so with that said, i've read that to fix the generic problem you can do the -Xlint:unchecked but that wont help me.
    and i've also read about raw types being the source of the problem, but now i'm completely stuck as to how to fix it. any suggestions would be greatly appreciated.
    here is my source code for the file that is causing this:
    import java.io.*;
    import java.util.*;
    public class OrderedLinkedList<T> implements OrderedListADT<T>
         private int size;
         private DoubleNode<T> current;
         public OrderedLinkedList()
              size = 0;
              current = null;
         public void add (T element)
              boolean notBegin = true;
              size = size + 1;
              if (isEmpty())
                   current = new DoubleNode<T>(element);
              while (current.getPrevious() != null)
                   current = current.getPrevious();
              Comparable<T> cElement = (Comparable<T>)element;
              while (cElement.compareTo(current.getElement()) > 0)
                   if (current.getNext() != null)
                        current = current.getNext();
                   else
                        notBegin = false;
              if (notBegin)
                   DoubleNode<T> temp = new DoubleNode<T>(element);
                   DoubleNode<T> temp2 = current;
                   current.setPrevious(temp);
                   temp.setNext(current);
                   temp.setPrevious(temp2.getPrevious());
                   current = temp;
              else
                   DoubleNode<T> temp = new DoubleNode<T>(element);
                   while (current.getPrevious() != null)
                        current = current.getPrevious();
                   temp.setNext(current);
                   current.setPrevious(temp);
         public T removeFirst()
              try
                   if (isEmpty())
                        throw new EmptyCollectionException("List");
              catch (EmptyCollectionException e)
                   System.out.println(e.getMessage());
              while (current.getPrevious() != null)
                   current = current.getPrevious();
              DoubleNode<T> temp = current;
              current = temp.getNext();
              size = size - 1;
              return temp.getElement();
         public T removeLast()
              try
                   if (isEmpty())
                        throw new EmptyCollectionException("List");
              catch (EmptyCollectionException e)
                   System.out.println(e.getMessage());
              while (current.getNext() != null)
                   current = current.getNext();
              DoubleNode<T> temp = current;
              current = temp.getPrevious();
              size = size - 1;
              return temp.getElement();
         public T first()
              try
                   if (isEmpty())
                        throw new EmptyCollectionException("List");
              catch (EmptyCollectionException e)
                   System.out.println(e.getMessage());
              while (current.getPrevious() != null)
                   current = current.getPrevious();
              return current.getElement();
         public T last()
              try
                   if (isEmpty())
                        throw new EmptyCollectionException("List");
              catch (EmptyCollectionException e)
                   System.out.println(e.getMessage());
              while (current.getNext() != null)
                   current = current.getNext();
              return current.getElement();
         public int size()
              return size;
         public boolean isEmpty()
              if (size == 0)
                   return true;
              else
                   return false;
         public String toString()
               return "An ordered linked list.";
    }the other file that uses this class:
    import java.io.*;
    import java.util.StringTokenizer;
    public class ListBuilder
         public static void main(String[] args) throws IOException
              String input;
              StringTokenizer st;
              int current;
              OrderedLinkedList<Integer> list = new OrderedLinkedList<Integer>();
              FileReader fr = new FileReader("C://TestData.txt");
              BufferedReader br = new BufferedReader(fr);
              input = br.readLine();
              while (input != null)
                   st = new StringTokenizer(input);
                   while (st.hasMoreTokens())
                        current = Integer.parseInt(st.nextToken());
                        if (current != -987654321)
                             list.add(current);
                        else
                             try
                                  unload(list);
                             catch(EmptyCollectionException f)
                                  System.out.println(f.getMessage());
                   input = br.readLine();
              System.out.println("Glad That's Over!");
         public static void unload(OrderedLinkedList<Integer> a) throws EmptyCollectionException
              while (!a.isEmpty())
                   System.out.println(a.removeFirst());
                   if (!a.isEmpty())
                        System.out.println(a.removeLast());
              System.out.println("That's all Folks!\n");
    }

    i tried @SuppressWarnings("unchecked") and that didnt work. so i ran it and it showed these errors:
    Exception in thread "main" java.lang.NullPointerException
    at OrderedLinkedList.add(OrderedLinkedList.java:30)
    at ListBuilder.main(ListBuilder.java:32)
    Press any key to continue...
    which would be:
              while (current.getPrevious() != null)and:
                             list.add(current);

  • Generics Problem

    I have a coding requirement that I would like to solve using generics, though I can't seem to get past an issue. My real example is way too complicated to post all of it here, but I'll try to simplify as much as possible (meaning, let's not get caught up in all the error checking, etc).
    I have an interface:
    public interface Function<T> {
         public void doSomething(T value);
    }Then, I have a class:
    public class LengthFunction implements Function<String> {
         private int length;
         public void doSomething(String value) {
              this.length += value.length();
         public int getLength() { return this.length; }
    }And, another class:
    public class CountFunction<T> implements Function<T> {
        private int count;
        public void doSomething(T value) {
            count++;
        public int getCount() { return this.count; }
    }Then, I have the workhorse:
    public class WorkHorse<C, T> {
        private HashMap<String, Function<T>> map;
        private Collection<C> list;
        public WorkHorse(Collection<C> incoming) {
            this.map = new HashMap<String, Function<T>>();
            this.list = new ArrayList<C>(incoming);
       public void addFunction(String key, Function<T> function) {
           this.map.put(key, function);
       public void process() {
            for (C element : this.list) {
                 for (String key : this.map.keySet()) {
                   for (Function<T> function : this.map.get(key)) {
                           T value = (T)magicalThingToGetValueFromElementOfTypeT(element, key);  // returns object
                        function.doSomething(value);
    }And finally, a process somewhere (let's play it this way):
    public void Process {
        public static void main(String[] args) {
             ArrayList<String> strings = new ArrayList<String>();
             // Fill array list with strings
             WorkHorse<String, Object> wh = new WorkHorse<String, Object>(strings);
             LengthFunction lf = new LengthFunction();
             CountFunction<Double> cf = new CountFunction<Double>();
             wh.addFunction("length", lf); // Compiler Error Here
             wh.addFunction("count", cf); // Compiler Error Here
             wh.process();
             System.out.println("Length: " + lf.getLength());
             System.out.println("Count: " + cf.getCount());       
    }The two places where it complains are noted, and it is complaining with:
    The method addFunction(String, Function<Object>) in the type WorkHorse<String,Object> is not applicable for the arguments (String, LengthFunction<String>)and
    The method addFunction(String, Function<Object>) in the type WorkHorse<String,Object> is not applicable for the arguments (String, CountFunction<Double>)So, I'm missing something... and I appear to be too stupid to figure it out.
    Any help is appreciated.
    Joe

    I haven't seen the "? super C" syntax. How is that different from "? extends C"?
    Since the function argument type will be the same as the collection contents type, there's no reason for having two type parameters on WorkhorseThere is. The first parameter on WorkHorse is the type of the elements in the collection. The second is the type of a value a given function is designed to work against. That value is obtained by invoking a method on each element in the list and then provided to the function. That is pretty complicatated, and relatively unimportant for this problem, so I elided with:
    T value = (T)magicalThingToGetValueFromElementOfTypeT(element, key);  // returns objectSo, therefore, if given an element of type MathThing in the WorkHorse, and that type has a method that returns a Double, then we'd want WorkHorse<MathThing, Double>. Likewise, if we have an element of type ColorThing, and it has a method that returns a Color, we'd want WorkHorse<ColorThing, Color>. Then, we'd have something like AFunction<Double> and BFunction<Color>.
    I was interested, though, and made a quick change to the WorkHorse class... and I'm still getting fundamentally the same error:
    The method addFunction(String, Function<? super Object>) in the type WorkHorse<String,Object> is not applicable for the arguments (String, Function<String>)When I instantiate WorkHorse with <String, Object>. Again, if I do WorkHorse<String, String> and use only implementations equivalent to Function<String>, I'm OK. However, if I do WorkHorse<String, Object> and have two Function<Strings> and one Function<Double>, I have the problem.

  • Quick mask problem in CS5

    I am a newbie and I have come across a problem which I cannot seem to rectify, when trying to use the quick mask tool and select the paintbrush it does not work, i.e no mask is created, can anyone help

    Adobe Acrobat doesn't have a  quick mask tool.

  • Quick time problem

    I am facing a problem with quick time. When i quit quick time player when movie is in progress, window gets closed however audio does not stop.
    Not sure what to do about it. Its happaning from the time i have updated my MBP to Lion.

    Download and use Flip4Mac.
    (31060)

  • Big generic problem

    I'm having a big problem. Look at this:
    public class Person {
    public class Employee extends Person {
    public class Bank {
    private List<Person> people = new ArrayList<Person>();
    public List<Person> people() {
    return people;
    Now the problem:
    public class Test {
    public static void main(String args[]) {
    Bank b = new Bank();
    b.people().add(new Employee()); // This was to be forbidden, but
    // the compiler doesn't complain
    In the class Bank I declared a list to contain only Person objects:
    private List<Person> people = new ArrayList<Person>();
    and a method to return it, with the same generic type:
    public List<Person> people()
    so, how could the compiler let me add a person of type Employee in the class
    Test? No matter if I change the declaration of the people() method to accept
    only objects of class Person and above, like this:
    public List<? super Person> people()
    it still compiles and I still can add Employee objects to the list that were
    supposed to hold just Person objects.
    So, the question is:
    HOW COULD I RETURN A LIST IN A METHOD WITH A GENERIC TYPE THAT HAS SUBCLASSES AND THAT I CAN MAKE THE LIST TO ACCEPT OBJECTS JUST OF THAT TYPE, NOT OF ITS SUBCLASSES? So, this is type safe. I don't want the list of the example above to accept objects of type Employee, just Person.

    so, why can I add Employees to a list declaredthis
    way
    public List<Person> get() {
    return people; // <--- list of Person
    }I don't see how it can be said any more clearly -
    BECAUSE AN Employee IS A Person!
    wouldn't it be to behave the same way as you
    said?
    Is
    the proplem related to a param and a return
    type.
    With params with works, but not with returntypes?
    I don't understand your point!Look if I'm really understanding this thing:
    public void add(List<Person> p)
    private List<Employee> e = new
    ArrayList<Employee>();
    obj.add(e) // Error. It's not exactly a PersonNow:
    public List<Person> people();
    private List<Employee> e = new
    ArrayList<Employee>();
    obj.people().add(new Employee()); // Ok. But why?An Employee is a Person. But in the first code it
    didn't let me add an Employee to a method acception a
    Person. That's what I want. But why the second code
    worked. Isn't it the same thing. The param type in
    both examples is a list of Person.To be more clear in the first code:
    public class Obj {
        public void people(List<Person> p)
    private List<Employee> e = new ArrayList<Employee>();
    Obj obj = new Obj();
    obj.people(e) // Error. It's not exactly a Person

  • XCelsius Generic Problems

    Hi everybody,
    I am a corporate user of Crystal XCelsius 2008, and after 3 months using it, I just wanted to post here my opinions on the product and also some of the (big amount) of problems I've found with it in hope they are solved in future versions of XCelsius.
    I find XCelsius a good product overall. It allows for fast deployment of interactive dashboards that are very appreciated by management, allowing for quick and informed decision taking. However, there are a number of issues and problems XCelsius needs to solve in order to become a trully powerfull dashboarding tool:
    - I am running XCelsius 2008 in a brand new Windows XP SP3 laptop with 4GB of RAM, Microsoft Office 2007 and all the latest patches applied. However, XCelsius keeps crashing regularly for no reason whatsoever ("Could not load localized strings" error, "Error loading Excel" error and others). I am now manually saving my work every 3 minutes, but it's a big inconvenience that XCelsius isn't reliable enough.
    - Every time I try to install a new XCelsius version/patch (XCelsius SP2 for instance), the setup stalls on registering modules and never finishes, having to manually kill the process. After much investigation (this forum helped a lot), I found out that this is due to incompatibility problems between XCelsius and McAfee Anti-virus which we use in our computers. This is the only software I've found problems with in this area, and anyway a corporation-oriented software should never ever have this kind of issues.
    - Erratic behaviour. This might seem very strange, but I've found myself (more than once) having to re-enter a formula in the worksheet in XCelsius because the first time it simply didn't work. The second time it works without a problem.
    - The Excel formula support is way too limited. Formulas like SEARCH or SUMIFS (new in Excel 2007) are critical in a data-driven enterprise environment, and not having them available is a big problem.
    - While trying to overcome the above limitation I installed the XCelsius Component SDK to see if I could replicate the functionality of those formulas with custom components. Problem is, although I am fluent in ActionScript and MXML, the SDK documentation is so sparse I could not even figure out how to write my own function.
    - Performance is another issue. When you use a reasonably-sized data set, XCelsius performance degrades exponentially, something not acceptable for corporate customers. I am not talking here about 10's of thousands of rows and columns, but simply 2,000 rows and 15 columns.
    I am simply posting thsi here in the hope that I am not the only user experiencing those issues and that BO listens to its customers and imporves the overall XCelsius experience in the near future.
    Thanks!

    Thanks for your reply and understanding, xzatious. Now I know I am not alone!
    I definately learnt to get used to those errors, but that doesn't make them less annyoing. As for the tip on the Excel process running in the background, thanks but I was already aware of it. But precisely this evidences one of the biggest problems with XCelsius: the poor coding techniques used when developing it.
    Any developer in the world knows one of the first things to implement in the application you are developing is good exception handling, and definately XCelsius is not an example of that. Most of the times, when it crashes, it simply says "Xcelsius.exe has found an error and needs to close" and then it leaves the Excel instance it has been using running in the background. What kind of exception handling is this?!?!? An enterprise-oriented software like XCelsius with its price tag (not cheap!) should be much better developed than this!
    And there's one last issue I forgot to mention, and that's more related to the selling/pricing area: when we bought XCelsius Engage (not Server), it came bundled with Flynet Web Services Generator. This is a very useful piece of software if you don't want/can't/don't have time to write web services yourself. But I had to speak witht eh Flynet guys themselves to discover my copy of Flynet wasn't working because it only works with an XCelsius Engage Server license! Why does it come bundled with regular XCelsius then?!?!?!?

  • Generic problem in passing arguments  ( question )

    I wrote some generic class that hold some generic map.
    When i looking some key in this map - i want to have the value of the key and set this value to some argument that i pass to the function that look for it.
    I dont want to return the value as return value of the function. ( i must have return value as Boolean )
    My paroblem is that when ever i call this function - i get back the "value" as null - and i dont know why and how to solve this problem.
    public abstract class SomeTable<T, U> extends Table{
          protected Map<T, U> currentTable = new HashMap<T, U>();
         public Boolean findItem(T itemToFind , U value){
                value = currentTable.get(itemToFind);
                return ( value == null );
    }Edited by: Yanshof on Dec 3, 2008 6:02 AM
    Edited by: Yanshof on Dec 3, 2008 6:11 AM

    You don't want to get the value from the key, you want to set it. Something more like:
           public Boolean findItem(T itemToFind , U value){
               currentTable.put(itemToFind, value);
               return (value == null);
            }Maybe you should review the functionality of a HashMap and variable assignment..
    Edit: Unless of course I'm misunderstanding your goal (ejp's post makes me think I am :) )
    Edited by: Pheer on Dec 2, 2008 10:15 PM

  • CS 4 Quick mask problem

    Hi
    My first posting here
    Cant figure out whats wrong. Maybe somebody here can guide me to a solution.
    When selected quick mask.
    Masking out an area and using brush to select area.
    When exiting the mask i dont get the marked area visible by the "walking ants" showing my what area i masked.
    Regardless of brush diameter.
    Before i had the same problem when opacity was set lower than for example 50. Setting the value higher solved the problem.
    Now opacity is 100 but i can see through the marked out area
    I have choosen red as color for masking area.
    Also the brush is "cut" in half when exceeding ca 300 in brush diameter.
    Any tips?
    AVFR

    Thanks for quick replies.
    Photoshop is updatet to latest version,
    Drivers are not possible to update yet. No newer that the one i am using.
    There exist one that nvidia says will fit to my laptop but get a fault message when installing.
    Will examine this further,
    Regarding if color i 100 % black.
    Where can i check this?
    When using cs 3 i couldnt see through the quick mask when opacity was 100.
    Now it seems like 50 %
    Regardless of which color i choose to use when brushing the areas i want selected
    Is there another menu option where i can check enter opacity?
    AVFR

  • Quick mask problem ..?

    When i use the quick mask tool  it always gives me the inverse selection .. ì have to invert it manually every time. what could be the problem ? any advice would be appreciated..

    Double click on the Quick Mask Symbol and make the switch.

  • Quick Zoom problem

    Has anyone else noticed that the Quick Zoom tool has stopped working, or is it just me? I've just gone from 7.2 to 8.2. 7.4 appears to work, and 8.1 doesn't. So this seems to be a "new feature"
    When I say "Quick Zoom", I mean Ctrl-Space, which used to give me a zoom cursor, with which I was able to drag a dotted box around the area I wanted to zoom in on.
    I didn't realise how much I used it until now.

    Andrew_Hart wrote:
    I thought Ctrl + Space to give you quick zoom (to an area outlined with a box of dotted lines) whilst staying in your original tool only worked in Ps (where it still works) but it was never available in CR.
    Have I been missing the utility of this very useful procedure in CR all these years?
    If you say, Yammer, that it worked in 7.4 then I suppose that must be so.
    Yep. It's been in ACR as long as I can remember. I've gone back to 7.2 because of this and the Bridge extractions problem.

  • Quick Look Problem ?

    I was just wondering why when I use QuickLook with 2 monitors, anything I QuickLook, picture/Music/Video etc.. always goes over to my other Screen I have hooked up to my iMac ? I want to quick Look it on the main screen (iMac Screen) not have to turn on the Other monitor every time I want to quick Look something. I think it's rather annoying having to turn that other monitor on all the time to view what I want, or drag it over to my main iMac screen to see. Now if I unplug the Other monitor it's just fine. Only when I have another monitor plugged in it does this.
    I looked up and down in the System Prefs to figure this out, and found Nothing. It's either a bad part on apples side, or there is something I"m missing.

    I had that same problem and I believe that I just "Quick Looked" a jpeg which opened on my other monitor (the wrong one) and while it was open moved the window to my main monitor. Once I did that I closed the quick look window and then reopened the same file using quick look again and I found it then opened on my main monitor.
    If this works then be carful about closing a quick look window. Make sure to close it on the monitor you want quick look to open on.

  • Quick string problem - takes 2 seconds lol

    hey guys, quick question...
    i am working on an email client, and through trial and error found out i need to use a strange string class, so i was wondering how do i declare this string in my constructor
    Parameters
    public void postMail( String recipients[ ], String subject, String message , String from) throws MessagingExceptionConstructor Line
    test.postMail([email protected], "test", "test2", "[email protected]");my problem is in that first [email protected]

    My first thought is to think that this isn't a string
    test.postMail([email protected], "test", "test2", "[email protected]");your method innvocation need to be the same as the declaration
    test.postMail([email protected], "test", "test2", "[email protected]"); // this is not the same
    public void postMail( String recipients[ ], String subject, String message , String from) // as thisSee how your first value you pass to the method isn't a string. Hope that helps!
    test.postMail("[email protected]", "test", "test2", "[email protected]"); // this is
    public void postMail( String recipients[ ], String subject, String message , String from) Edited by: didittoday on Mar 8, 2008 8:12 PM

  • Generic problem: superdrive malfunction - trying to eject disk?

    I've previously posted this problem on the Powerbook thread, but following my initial problem of not getting a stuck disk out of the superdrive, this seems to have progressed into a more serious issue which may be solved using a generic OSX solution?
    It started a stuck disk in the drive, so I used a credit card and a sharp, very thin letter opener (metal) and used them like tweezers to force the disk out.
    However, now I'm left with a machine that continually tries to eject an imaginary disk, over and over again. But the disk has been forced out by myself.
    Can I over-ride this command in Terminal or somewhere else?
    Thank you in advance.
    C Marks

    Hi Carey,
    Optical drives are all too short lived i my experiences, Slot loading ones the worst.
    This may take a few back & forths, but...
    In Finder, select Go menu>Go to Folder, and go to "/volumes".
    Volumes is where an alias to your hard drive ("/" at boot) is placed at startup, and where all the "mount points" for auxiliary drives are created for you to access them. This folder is normally hidden from view.
    Drives with an extra 1 on the end have a side-effect of mounting a drive with the same name as the system already think exists. Try trashing the duplicates with a 1 or 2 if there are no real files in them, and reboot.
    Safe Boot , (holding Shift key down at bootup), use Disk Utility from there to Repair Permissions, test if things work OK in Safe Mode.
    Then move these files to the Desktop...
    /Users/YourUserName/Library/Preferences/com.apple.finder.plist
    /Users/YourUserName/Library/Preferences/com.apple.systempreferences.plist
    /Users/YourUserName/Library/Preferences/com.apple.desktop.plist
    /Users/YourUserName/Library/Preferences/com.apple.recentitems.plist
    Reboot & test.

  • Intricate generics problem

    I have a problem with this code:
    interface Test<T extends Number> {
        public <U> void test(Test<? super U> t, U value);
    }Sun javac 1.6.0_13 accepts it, Eclipse 3.5.1 says:
    "Bound mismatch: The type ? super U is not a valid substitute for the bounded parameter <T extends
    Number> of the type Test<T>". This, at first glance, seems a valid complaint, but on the other hand, how could there ever be a parameterized Test that is not of type <T extends Number> passed as a parameter?
    So, my question is: which one is right? I suspect javac is correct and Eclipse has a bug here, but why? Is it correct of the compiler to rely on the implicit bounding of all Test objects as defined in the interface declaration to <T extends Number>, or is it correct (as Eclipse's syntax checker does) to demand an explicit one?
    Could anyone point me to the appropriate Java reference paragraph covering this situation?
    Anyway, either javac or Eclipse has a bug here obviously, I'm just not sure which one!
    Edited by: marhaus on Nov 4, 2009 6:50 AM

    What confuses me is why Javac might accept that, but neither accepts this:
    public interface Test<T extends Number> {
        public <V> void test(Test<V> t);
    }If this doesn't work, I certainly understand why your example doesn't. I think it probably has to do with how the V (or U in your case) is inferred, and that's why they differ.
    For instance, consider this:
       public static void main(String[] args) {
          String s = getSomeObj();
       private static <V> V getSomeObj() {
          return null;
       }Here, V is inferred by the fact that you're assigning to a String. Everything compiles fine. But now consider if we changed getSomeObj:
       private static <V> V getSomeObj(SomeClass<V> sc) {
          return null;
       private static class SomeClass<V extends Number> {
       }This now doesn't compile, perhaps because the bounds of V are not determined at all by how it is used in the arguments. Similarly,
       private static <V> SomeClass<V> getSomeObj() {
       Won't compile, for the same reason. So it seems like neither goes into the decision-making of the bounds of V. It seems like the bounds are decided completely by what's in the <V> part.
    If this is all for learning, by all means continue. I would almost say that it's a javac anomaly. But if it's an actual problem, I would say just do this instead:
        public <V extends Number, U extends V> void test(Test<V> t, U value);

Maybe you are looking for