Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Something similar to C# .NET Generic List in java

Tags:

java

c#

.net

How would be a c# .net generic list in java?

somthing like that:

public class ClientList : List<Client> { }

the answer from Nikil was perfect, I just want to add to whoever wants to create a class from the List:

public class ClientList extends ArrayList<Client>
like image 535
RollRoll Avatar asked Apr 12 '12 00:04

RollRoll


People also ask

Which is similar to C?

Javascript has a reasonably C-like syntax, and it's a very popular language. Javascript has a lot of quirks, but it has one powerful similarity to C - it's simple. The complete Javascript specification is very short, and the language is very powerful and high-level.

What language can replace C?

To overcome such issues, Microsoft developers recently announced to use the Rust programming language instead of C and C++ to write and code the components of Windows. The project is known as Verona where the developers will develop a new and safer programming language for Windows.

Is C similar to Pascal?

Pascal procedures are considered equivalent to C "void" functions, and Pascal functions are equivalent to C functions that return a value.

What is Objective-C similar to?

The best alternative is Python, which is both free and Open Source. Other great apps like Objective-C are JavaScript, Java, C++ and C (programming language). Objective-C alternatives are mainly Programming Languages but may also be Game Development Tools.


1 Answers

Java's List interface (java.util.List) can be generified. In other words, instances of List can be given a type, so only instances of that type can be inserted and read from that List. Here is an example:

List<String> list = new ArrayList<String>();

This list is now targeted at only String instances, meaning only String instances can be put into this list. If you try to put something else into this List, the compiler will complain.

The generic type checks only exists at compile time. At runtime it is possible to tweak your code so that a String List has other objects that String's inserted. This is a bad idea, though.

Accessing a Generic List

You can get and insert the elements of a generic List like this:

List<String> list = new ArrayList<String>();

String string1 = "a string";
list.add(string1);

String string2 = list.get(0);

Notice how it is not necessary to cast the object obtained from the List.get() method call, as is normally necessary. The compiler knows that this List can only contain String instances, so casts are not necessary.

Iterating a Generic List

You can iterate a generic List using an iterator, like this:

List<String> list = new ArrayList<String>();

Iterator<String> iterator = list.iterator();

while(iterator.hasNext()){
    String aString = iterator.next();
}

Notice how it is not necessary to cast the object returned from the iterator.next() next call. Because the List is generified (has a type), the compiler knows that it contains String instances. Therefore it is not necessary to cast the objects obtained from it, even if it comes from its Iterator.

You can also use the new for-loop, like this:

List<String> list = new ArrayList<String>();

for(String aString : list) {
    System.out.println(aString);
}

Notice how a String variable is declared inside the parantheses of the for-loop. For each iteration (each element in the List) this variable contains the current element (current String).

like image 53
Nikhil Agrawal Avatar answered Oct 27 '22 21:10

Nikhil Agrawal