How can I write a program that compiles without warnings?

I tried the following with the 1.5 beta-compiler (build 28; I think):
class Y {
     public static final class Pair<X,Y> {
       private X fst;
       private Y snd;
       public Pair(X fst, Y snd) {this.fst=fst; this.snd=snd;}
       public X getFirst() { return fst; }
       public Y getSecond() { return snd; }
       public String toString() { return "("+fst+","+snd+")"; }
  public static void main(String... args) {
     Pair[] pairArr = new Pair[10];              // supposed to be an array of Pair<Integer,Integer>
     for (int i=0; i<pairArr.length; i++)
         pairArr[i] = new Pair<Integer,Integer>(i,i);
     for (int i=0; i<pairArr.length; i++) {
         Pair<Integer,Integer> p = pairArr; // unchecked warning
     System.out.println(p);
     Integer first = p.getFirst();
     Integer second = p.getSecond();
// ... more stuff ...
It turns out that I get an unchecked warning when I extract an element from the array of pairs. Okay, that's fine. How can I avoid the warning? I had expected that an explicit cast would help.
  Pair<Integer,Integer> p = (Pair<Integer,Integer> )pairArr;
With a cast I'm telling the compiler: "I _know_ what I'm doing; please trust me." But the compiler still issues a warning.
How can I write a warning-free program in this case? The only thing I can think of, is not using the parameterized type Pair in its parameterized form. But it's not the idea of Java Generics that I refrain from using parameterized types. What am I missing?

It turns out that I get an unchecked warning when I
extract an element from the array of pairs. Okay,
that's fine. How can I avoid the warning? I had
expected that an explicit cast would help.
Pair<Integer,Integer> p = (Pair<Integer,Integer>
)pairArr;
With a cast I'm telling the compiler: "I _know_ what
I'm doing; please trust me."  But the compiler still
issues a warning.  Yes, but at least you were able to change the warning from "unchecked assignment" to "unchecked cast" which is a little shorter ;-)
Seriously , since arrays of generic types are disallowed, there is probably no way to get rid of these warnings - which makes a strong point for eliminating "unchecked" warnings altogether (see the other thread "selectively suppressing compiler warnings")
Cheerio,
Gernot

Similar Messages

  • How can I install a program that comes in a CD when the iMac does not come with a CD entrance?

    How can I install a program that comes in a CD when the iMac does not come with a CD entrance?

    Another option if you have access to a second Mac with a drive is to connect the two together with the iMac in Target Disk Mode,  Transferring files between two computers using FireWire, and installing from the Mac with the optical drive.
    OT

  • I have given a laptop to my grandson without deauthorizing itunes. How can I deauthorize itunes on that computer without being in possision of it?

    I have given a laptop to my grandson without deauthorizing itunes. How can I deauthorize itunes on that computer without being in possision of it?

    You cannot, nor do you need to until you reach the limit of 5.  Then you can deauthorize all and authorize only the machines you choose.

  • How can I write the program

    I would like to know how can i Change the program so that the Message'What is the sum of X and Y' can be shown in a GUI after I click the Start Button.
    The program is shown below. Please help me,Thank You Very Much!
    import java.awt.*;
    import java.awt.event.*;
    import javax.swing.*;
    public class client2 {
    public static void main(String[] args){
    Frame f = new ClientFrame("Client");
    f.setSize(400,400);
    f.setVisible(true);
    class ClientFrame extends JFrame{
    JPanel StartPanel = new JPanel();
    JPanel OutputPanel = new JPanel();
    String Question;
    JLabel label=new JLabel(Question);
    Button GameStart = new Button("Start the Game");     
    Container c=this.getContentPane();
    public ClientFrame(String title){
    super (title);
    c.setLayout (new BorderLayout());
    StartPanel.add(GameStart);
    OutputPanel.add(label);
    c.add(StartPanel,BorderLayout.NORTH);
    c.add(OutputPanel,BorderLayout.CENTER);
    GameStart.addActionListener(new ActionListener() {
    public void actionPerformed(ActionEvent Evt) {
    GameStart.setEnabled(false);     
    Integer x=10;
    Integer y=20;
    Question ="What is the sum of "+x+ "and" +y;
    System.out.println("Java rules!");
    addWindowListener(new WindowAdapter() {
    public void windowClosing(WindowEvent evt) {
    System.exit(0);
    }

    This code won't even compile.
    Anyway, you should set the field Question's value to the String, when you declare it, before you use it in the label.
    Also, you should name your local variables with names that start with lower-case letters. That's one of Java's naming conventions, and it makes your code easier to read.

  • Can Anyone Write a Program that does this?

    Once again , I know it's simple but I unfortunatly can't accomplish this task if anyone can write this program for me with the use of simple recursive arguements I would appreciate it..
    pre: n greater than or equal to 0
    post: return true if n is prime and false otherwise
    thats the problem

    I scrapped working on that one , heres what im doing now..public class plus251
    We are to Recursively add well you can read the pre and post conditions, now im basically supposed to follow this same setup which I wrote out , but now im too multiply by adding and I cant figure out the recursive part I know that the base case is if a == 1 return b , but its the else which I have no clue how to write..
    public static void main(String[]args)
         /* pre: a and b are positive integers
         post: outputs a + b by adding 1 to b, a times
         int a, b;
         System.out.println("enter an integer");
    a = Console.readInt();
    System.out.println("enter an integer");
    b = Console.readInt();
         System.out.println("a+b = "+ plus(a,b));
    public static int plus(int a, int b)
         /* pre: a and b are positive ints
         post: returns a + b
         if (a==0)
         return(b); /* this will add the accumulated
    ones to b to get the total */
         else
         return(1+plus(a-1, b)); /*this returns 1 until a
    becomes 0 */
         a is 0 ---- return b
         plus(b, k) ---- returns mathplus(b, k)
         plus(b, k+1) --- returns mathplus(b, k+1)
    plus(b,k+1) ---- returns 1+ plus(b, k)
    |
         |          1+ mathplus(b, k)
    |
         ------------------ mathplus(b, k+1)

  • How can I write a code that triggers when clicking on Save button

    I want to hide certain fields inside my tracking list, if the login user is not part of the “Managers” group.
    To do so I want to add the following Page-load code inside a web part :-
    using (SPSite site = new SPSite(SPContext.Current.Site.Url))
    using (SPWeb web = site.OpenWeb())
    {SPGroup group = site.Groups["Managers"];
    bool isUser = site.IsCurrentUserMemberOfGroup(group.ID);
    if (isUser)
    {textbox.Visible=false;}}
    And the following custom code that triggers when saving the form :-
    using (SPSite site = new SPSite(SPContext.Current.Site.Url))
    using (SPWeb web = site.OpenWeb())
    SPList list = web.Lists["ListName"];web.AllowUnsafeUpdates = true;
    SPListItem item = list.Items[SPContext.Current.ListItem.UniqueId];
    txtTitle.Text = item["Title"] as string;item.Update();web.AllowUnsafeUpdates = false;
    But I am not sure where exactly I should add the custom code for the save button ? I tried writing it inside the Oninit method , but I got many errors as shown below:-
    So can anyone advice how i should write the web part to include the Page load & the custom save button code?
    second question in general is my above approach considered valid , i mean to hide/show list columns using web parts ?

    Hi,
    According to your post, my understanding is that you want to hide/show list columns based on specify the permission for Users, SharePoint Groups or Active Directory Groups.
    Here is a solution from CodePlex for your reference:
    SharePoint 2013 Column & View Permission
    https://sp2013columnpermission.codeplex.com/
    Best Regards
    Dennis Guo
    TechNet Community Support

  • How can we write BDC program to upload data in to line items(VA01) ?

    Error in processing BDC table control program...
    How can i populate data through bdc program dynamically, when some of the lines are in disable mode....
    for e.g ...
    in va01 transaction code ....
    1 to 5 lines are not in disable mode....
    but 5 to 10 lines are in disable mode...
    so hw u process those remaining disable lines at runtime and populate data in to the fields of line items...through bdc program..

    hi..friends.,
    yea actually this question has been raised by interviewer ...
    but just the same i replied as you did...
    but he told that we can upload the data....towards in to disabled line items
    at run time..with using change trasaction va02..with implementing bdc programm...
    so i just want to know the steps of how to perform it..
    Can anyone....my friends..
    byeeee.....

  • How can i write Recursive Program in PL/SQL

    Hi, i am new to Pl/sql Programming can any one suggest me with an example.
    I need to implenet this logic in one of my Procedure functionality

    I have to find whether my Userkey belongs to some groups and in that Gr
    oups there can be many more subgroup which means my user is indirectly
    associated with all Groups and Sub Groups so i need to check with the levels
    till the time there is no SubGroups associated with my the Main Group G1 are
    there.Seems you just need the hierarchy query:
    SQL> SELECT groups.grp, level
      2  from
      3  (
      4  select 'Group' grp, null parent from dual
      5  union all
      6  select 'Subgroup 1', 'Group' from dual
      7  union all
      8  select 'Subgroup 2', 'Group' from dual
      9  union all
    10  select 'SubSub 1', 'Subgroup 2' from dual
    11  union all
    12  select 'SubSub 2', 'Subgroup 1' from dual
    13  ) groups
    14  start with grp = (select grp from
    15  (
    16   select 'Key1' key, 'Subgroup 1' grp from dual
    17   union all
    18   select 'Key2' key, 'Subgroup 2' grp from dual
    19  ) userkeys
    20  where key = 'Key1'
    21  )
    22  connect by prior groups.grp = groups.parent
    23  /
    GRP             LEVEL
    Subgroup 1          1
    SubSub 2            2Rgds.

  • How can i disable some programs that i am not using

    I have so many programs on my computer, I don't know how to disable them, can you help?, thanks

    click start button
    click control panel
    click programs and features
    then uninstall what programs you no longer need
    Probably would have received an answer faster if you had posted this in desktop area

  • A gap between letters started to show in my indesgn files. tried to undo with tracking, the paragraph settings. nothing helped. it ruins the fluent view of the document. how can i restore the program so that doesn't happen?

    a gap between letters started to show in my indesign files. tried to undo with tracking, then with the paragraph settings. nothing helped. it ruins the fluent view of the document. how can i restore the program so that doesn't happen?
    for example: say i write a paragraph. then, in a weird some sort of way an involuntary gap suddenly appears between different letters of random words(i did not recognize any pattern to the gap appearing) throughout the entire paragraph. once i've double clicked on that paragraph, and made a minor change, lets say tapped a 'space' key, and then clicked ctrl+z to undo, it has aligned(or fixed) the entire paragraph and made it look ok again. i've tried numerous ways to undo the entire thing, but cannot find the reason. i've been working for a few years and there's no reason why this thing all of a sudden should happen right now.
    if anyone has stumbled on something like that and can advise, i would welcome it.
    MNS-KG
    Vadim

    yep...i'm typing with hebrew. i'll make a printscreen with the settings you've asked for@:
    if you look closely you'll see that almost every line has a single letter apart. in hebrew there is no usually a singe letter structured words.

  • How can I write program in Labview for zbrain kit..??

    Hello everybody
    I need to write the program in Labview fro Zbrain kit. I need to write code for multiple analog input (through multiple channels) that results multiple digital output. I found one refrence code which has one analog input (one channel) and gives one digital output.
    I also need to save data for channels. I have no idea how can I save data for channels.
    I have attached here the base program. Please give me idea how can I understand this program so that I can complete following tasks:
    1. Save data for one channel and later for multiple channels
    2. Modify the number of channels from one channels to multiple channels.
    Actually I have gone through the videos and text based content(basics in labview) but I am unable to write the code.
    Attachments:
    Analog_3_InBlock.vi ‏131 KB

    Hi,
    Attached is an example snipped with some more comments on how to acquire multiple channels on a ZBrain platform that is based on an Analog Devices Blackfin processor. Just drag and drop it into your LabVIEW diagram. More examples can be found here: http://wiki.schmid-engineering.ch/zsystem/doku.php?id=example_zmc_adc-dma_continous. The idea behind dynamic data acquisition on a microprocessor is simple: you define the channels (up to 6x) , sampling frequency (up to 250kHz)  and block sizes (up to 65'000) and start the acquisition. The underlying driver model samples in the data in parallel and the LabVIEW program gets this data. While e.g. processing the data, the underlying driver model continues sampling new data.
    I hope this helped.
    Marco Schmid
    Schmid Engineering
    Attachments:
    snippet_zmc_adc-dma_continuous.png ‏107 KB

  • I am using a code based typesetting program (not WYSISYG) that outputs PDFs. I am producing 100 plus pages that have multiple graphics on each page. I need to know how to format a PDF command that I can incllude in my programming that will tag my graphics

    I am using a code based typesetting program (not WYSISYG) that outputs PDFs. I am producing 100 plus pages that have multiple graphics on each page. I need to know how to format a PDF command that I can incllude in my programming that will tag my graphics with "Alternative Text".
    I know that with a Microsoft product graphics can be tagged before a PDF is made. I need to know how to do this with my programming.

    The Acrobat SDK might be a starting point.
    From there, perhaps a plug-in (built with C+).
    Perhaps with a licensed release of a PDF Library (this could be $$).
    The viable and cost effective alternative is use the tried and true.
    Authoring in an appropriate authoring application with appropriate tag management.
    Example:  Adobe InDesign; Adobe FrameMaker or MS Word with PDFMaker (comes with install of Acrobat).
    This way you place "Alternative Text" when mastering content in the authoring file.
    Going the route and with some look-see (research) you may find programmatic approaches to placing the alt txt in the authoring file.
    Note: as discussed in the Matterhorn Protocols there is no programmatic method that provides a fully accessible PDF (specifically, that is an ISO 14289-1, PDF/UA-1 compliant PDF).
    Regardless, here you have a sub-forum for discussions on Acrobat usage.
    Consequently discussions on/of 3rd party software is rather out of scope eh.
    Be well...

  • How can I organise the program dashboard the way that it does not open automatically and very quickly other programs?

    How can I organise the program dashboard the way that it does not open automatically and very quickly other programs?
    Thanks for an answer
    Reinhard Voegele
    Switzerland

    If you hold down the option key while in dashboard you can close many of the programs.
    To disable it from opening automatically, or unintentionally:
    1) go to System preferences -> mission control.
    2) Deselect any mouse shortcuts
    3) Remove it from hot corners, if it is active.

  • I downloaded the free trial for Adobe XI Pro but now I cant find it on my computer or figure out how to use it. The only thing I see that downloaded is Adobe Download Assistant. How can I access the program?

    I downloaded the free trial for Adobe XI Pro but now I cant find it on my computer or figure out how to use it. The only thing I see that downloaded is Adobe Download Assistant. How can I access the program?

    Assistanttolindsey the Adobe Download Assistant will download the installation files for Adobe Acrobat Professional XI.  By default they are saved to your download folder.  You can begin the installation process from there.

  • I am trying to burn a .mov file from fcp xpress timeline to a dvd.  The popup message I receive prior to inserting the dvd is that I need 20.05 gb of space available on dvd (a 1.25 min). How can I get this program onto a dvd that shows a max of 8.5gb?.

    I am trying to burn a .mov file from fcp xpress timeline to a dvd.  The popup message I receive prior to inserting the dvd is that I need 20.05 gb of space available on dvd (a 1.25 min program). How can I get this program onto a dvd (where the max available gb's  for off the shelf dvds-r)  shows a max of 8.5gb?.  Please help.  Thank You!

    You have posted your question in the Final Cut  Pro X forum. You want to be in this forum instead; https://discussions.apple.com/community/professional_applications/final_cut_expr ess_hd
    Good luck.
    Russ

Maybe you are looking for