Understanding List Array in Java Tutorial



In this tutorial you're going to learn how to use lists in the Java code, So let's first see list declaration for list that will hold string elements inside our main class we'll create a "List' class example's, Notice that this interface is from java package once we imported it, Remember how array have a fixed size, We will change the size of an array but "List" class will automatically resize if we try to add more elements that it can currently hold you can look at list as dynamic array, List uses generic or in other words we can specify what type of objects we want to store in our list inside the () angle bracket we can specify the type, So list class or List interface defines a lot of useful methods, For example we can insert elements in the middle of the list we can find elements in least we can convert List to Array and so on, Buts le'ts first see this example with 'List' With List that hold String, And now our List we want to hold all our soldiers or in this case we will just hold names, So we will declare it like Listnames = new ArrayList() So now we have empty list and this is again polymorphism in actions, We are using interface as a reference type but our object in memory will be 'ArrayList' So List is really helpful when we dont know how many elements do we need, But first let's get some elements to our List like

COMPLETE CODES:

public class ListDemo{
public static void main(String[] args){

List<String>names = new ArrayList<>();
names.add("Jimmy");
names.add("Anthony");
names.add("Jhon");
for(String name: names){
System.out.println("names=" +name + "uppercase ="+name.toUpperCase());
}
Soldier pikeman = new Pikeman(100,100);
pikeman.run();
pikeman.setDamage(15);

Soldier archer = new Archer(100,5);
archer.run();
archer.setDamage(10);

Soldier pistolero = new Pistolero(100);
pistolero.run();
pistolero.setDamage(20);

List<Soldier> army = new ArrayList<>();
army.add(pikeman);
army.add(archer);
army.add(pistolero);

for(Soldier soldier: army){
System.out.println("soldier=" +soldier.getClass().getSimpleName()+ "health=" +soldier.getHealth() +"damage=" +soldier.getDamage()); 
}
}


And now list can also be used for ritual so to bring list we can use a for-each loop and we can use an index to get or let's say to get the element a specific index There is a get method but for now let's use a (For-Each-Loop), Later we will see a For-loop by using the index so  type the code below

for(String name: names){
System.out.println("name=" +name + "uppercase =" +name.toUpperCase());
}

And this will print our names in upperCase, So if you notice it print name "JIMMY" uppercase, "ANTHONY" in upperCase, "JOHN" in uppercase, So the list is very helpful when we don't know how many elements do we need if we use Array with the size of 5 We can only store 5 elements in that array, That is where the list can be very helpful, We want to make an army of soldiers but we don't know How many soldiers each player has or in other words how many soldiers each player will play So we will use List with type soldier, Why with type  Soldier, since soldier is a parent class within our example, and in that way, we can add any type of soldiers to list, In other words, we can add any subclass of soldier class in our example we have Archer, pikemanpistolero So right below we can declare List<Soldier> army = new ArrayList<>(), and again we can type army.add(pikeman); With List<> we can add All type of soldier-like army.add(archer);
army.add(pistolero); and now if we want to print all the soldiers in our army we can again use 

for(Soldier soldier: army){
System.out.println("soldier=" +soldier+ "health=" +soldier.getHealth() +"damage=" +soldier.getDamage()); 
}  
And this will print a soldier, health, and damage, And now to print using normal for(){}  with the index we can type

for(int i = 0;i<army.size(); i++){
Soldier soldier = army.get(i);
}   

and we want to print just a soldier so type
System.out.println("soldier =" +soldier.getClass().getSimpleName());
So that is all I can have for this tutorial






Post a Comment

0 Comments