Results 1 to 5 of 5

Thread: Generics problem in java

  1. #1
    Join Date
    Mar 2010
    Posts
    203

    Generics problem in java

    Hello
    I have a problem with my code, please check if this is correct and see if you can explain me this.
    Here is the code:
    Code:
    <person> Comparator com = ascending? 
    New compersonne()Collectibles.reveseOrder(New compersonne());
    If you can explain me how this code works then you please explain me.

  2. #2
    Join Date
    Nov 2009
    Posts
    583

    Re: Generics problem in java

    Hello,
    See the following
    Code:
     condition? Value_if_true: Value_if_false
    So in your case whether ascending is true then c will New ComparePersonne() Otherwise it will Collections.reveseOrder (new ComparePersonne ()).
    With if traditional one would write this:
    Here is the code
    Code:
    	Comparator <person> c;
    	if (ascending) {
    		c = New ComparePersonne();
    	} else {
    		c = Collections.reveseOrder(New ComparePersonne());
    	}

  3. #3
    Join Date
    Nov 2009
    Posts
    343

    Re: Generics problem in java

    Hello,
    You're in the presence of a conditional operator.
    Here is a small example
    Code:
    max = (a> b) ? a: b
    is equivalent to the statement
    Code:
    if (a> b)
        max = a;
    else
        max = b;
    This small, simple example will help you understand what's happening at home.

  4. #4
    Join Date
    Nov 2009
    Posts
    359

    Re: Generics problem in java

    Hello,
    It is a simplified syntax of "if then else" for simple cases:
    Here is the code for the same
    Code:
    condition? instruction if it is true: if statement
    If you do have any more problem regarding this then you can post back here. But before this try this in your code and if you have any more problems then do post back.

  5. #5
    Join Date
    Nov 2009
    Posts
    330

    Re: Generics problem in java

    Hello,
    See, if this can help you
    Code:
    List<String>[] lst = new List<String>[10]; // illegal
    Object[] obj = lst;  // OK because List<String> is a subtype of Object
    List<Integer> li = new ArrayList<Integer>();
    li.add(new Integer(3));
    obj[0] = li; 
    String str = lst[0].get(0);

Similar Threads

  1. Generics vs. Reflection in Java
    By Jagruti23 in forum Software Development
    Replies: 9
    Last Post: 15-09-2010, 09:36 PM
  2. Java JDK problem
    By Samara in forum Software Development
    Replies: 6
    Last Post: 19-07-2010, 01:02 PM
  3. Getting warning in java generics
    By Angelica Maria in forum Software Development
    Replies: 5
    Last Post: 22-03-2010, 01:45 PM
  4. Java Problem
    By suhjonathan in forum Windows Software
    Replies: 5
    Last Post: 12-11-2009, 09:48 AM
  5. Problem With Java
    By Kusagra in forum Software Development
    Replies: 2
    Last Post: 17-03-2009, 11:46 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,713,982,970.84930 seconds with 16 queries