-1

I wonder what really means these signs and their data types inside them? I saw a lot of code like this:

ArrayAdapter<String> adapter =  new ArrayAdapter<String> ( this, android.R.layout.simple_list_item_1, names );

There are a lot of code in java and android and i think c# that using this type of declaring objects. I want to know what is these declaring types and what is the usage of theme? Thanks in advance.

Majid Daeinejad
  • 1,037
  • 8
  • 19

3 Answers3

2

They declare the type of object that the code (in this case ArrayAdapter) will be holding. Generics in java allows for some code to hold potentially any type of object, but in general if you can specify what type you want the code to hold you should. Take an ArrayList for Example:

Arraylist<String> myList = new ArrayList<String>() //Holds Strings
Arraylist<Integer> myList = new ArrayList<Integer>() //Holds ints
Arraylist<aCustomClass> myList = new ArrayList<aCustomClass>() //Holds objects of a class I made called aCustomClass
Arraylist myList = new ArrayList()  //Is set to recieve any type of object

If you do not specify what type of object you want, then it is expecting a "raw type" which is considered unsafe/bad practice. To learn more search for Generics in Java

Levenal
  • 3,796
  • 3
  • 24
  • 29
0

<> is used to type a generic class. A common example would be lists or maps .

So the source code of a list looks like this:

public class MyList<T> extends List<T> 

You can then create methods like this:

public void add(T value)

To learn more about generics, you can check out the official guide here: http://docs.oracle.com/javase/tutorial/java/generics/ (java)

therealtbs
  • 162
  • 10
0

Suppose you want to display data in a list view here Array Adapter is used

Adapter inflate the layout for each row in ListView

here adapter is the object of ArrayAdapter class for inflating layout for each row. String is define List having String type data. this define the current state of object android.R.layout.simple_list_item_1 is system generated resource. name is define array of string which wil be deplay in the ListView

Gajendra Rawat
  • 3,673
  • 2
  • 19
  • 36