473,401 Members | 2,127 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,401 software developers and data experts.

Generics Hmmmmm easy yet i don't understand :)

gautamz07
Expand|Select|Wrap|Line Numbers
  1. import java.util.*;
  2.  
  3. public class SetDemo {
  4.  
  5.   public static void main(String args[]) { 
  6.      int count[] = {34, 22,10,60,30,22};
  7.      Set<Integer> set = new HashSet<Integer>(); // What does this line really tell the compiler 
  8.      try{
  9.         for(int i = 0; i<5; i++){
  10.            set.add(count[i]);
  11.         }
  12.         System.out.println(set);
  13.  
  14.      }
  15.      catch(Exception e){}
  16.   }
  17.  
  18.  
Set<Integer> set = new HashSet<Integer>();

What Does The Abouve Line really do or tell the compiler .

Expand|Select|Wrap|Line Numbers
  1. public class GenericMethodTest
  2. {
  3.  
  4.    public static < E > void printArray( E[] inputArray )
  5.    {
  6.       // Display array elements              
  7.          for ( E element : inputArray ){        
  8.             System.out.printf( "%s ", element );
  9.          }
  10.          System.out.println();
  11.     }
  12.  
  13.     public static void main( String args[] )
  14.     {
  15.         // Create arrays of Integer, Double and Character
  16.         Integer[] intArray = { 1, 2, 3, 4, 5 };
  17.         Double[] doubleArray = { 1.1, 2.2, 3.3, 4.4 };
  18.         Character[] charArray = { 'H', 'E', 'L', 'L', 'O' };
  19.  
  20.         System.out.println( "Array integerArray contains:" );
  21.         printArray( intArray  ); 
  22.  
  23.         System.out.println( "\nArray doubleArray contains:" );
  24.         printArray( doubleArray ); 
  25.  
  26.         System.out.println( "\nArray characterArray contains:" );
  27.         printArray( charArray ); 
  28.     } 
  29. }
  30.  

public static < E > void printArray( E[] inputArray )

I Understand that becasue of the above generic meathod we don't have to write 3 overloaded meathods to print out an array of characters , integers and Double . But what does it really tell the compiler .
Dec 7 '13 #1
1 1230
r035198x
13,262 8TB
It tells the compiler that the set only contains objects of type Integer so there will be compilation errors if you try to add e.g Strings to the set.
Dec 11 '13 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: Merrua | last post by:
I dont understand how to do this I created a dialog based project and that is fine for most of my programming project, but one dialog box is going to generate a graph, and as far as i know from...
11
by: Mark Goldin | last post by:
Very simplified: namespace humanres { public class main2 : System.Web.UI.Page {
3
by: Sheau Wei | last post by:
This is the code the code that i try to run it as my search engine for my database. But i dont understand why i run a notice that undefind index. Can u help me ? <form method="post"...
5
by: ryann18 | last post by:
I was just wondering, my teacher assigned a project for me and told me to "create one cat in SandBox and put all the methods (for example method_1) you need for the excercises in KittyKitty and call...
19
by: so many sites so little time | last post by:
the table is head the colunm is called body <?php //show_site.php // This script retrieves blog entries from the database. // Address error handing. ini_set ('display_errors', 1);...
8
by: mohammaditraders | last post by:
#include <iostream.h> #include <stdlib.h> #include <conio.h> #include <string.h> class Matrix { private : int numRows, numCols ; int elements ;
6
by: Bishop | last post by:
a += (UInt32)(url + (url << 8) + (url << 16) + (url << 24)); I'm trying to understand what the above line of code does. I understand that the url part of (url << 8) returns part of the string...
5
by: dynamo | last post by:
In the following program i dont how cin knows where day, stops and ch ,starts. The same thing for month and year. When you type the input string without spaces.Can anyone help?thanks #include...
3
by: canabatz | last post by:
Hello all!! i got a small problem ,not big :) i got this example of a link in my site that display everything correctly! http://www.mysite.com/product.php?id=200 now my problem is : if...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.