package org.JavaIncloud.java ;
import java.util.ArrayList ;
import java.util.List ;
public class CommonOpetationWithList
{
public static void main
( String ...
JavaInCloud ) {
List< String> luxuryCars = new ArrayList< String> ( ) ;
//List allows duplicate element to add
luxuryCars.add ( "BMW" ) ; luxuryCars.add ( "Audi" ) ;
luxuryCars.add ( "MercedesBenz" ) ; luxuryCars.add ( "Porsche" ) ;
luxuryCars.add ( "Jaguar" ) ; luxuryCars.add ( "Duesenberg" ) ;
luxuryCars.add ( "RollsRoyce" ) ; luxuryCars.add ( "RollsRoyce" ) ;
System .
out .
println ( "luxuryCars>>" + luxuryCars
) ; //luxuryCars>>[BMW, Audi, MercedesBenz, Porsche, Jaguar, Duesenberg, RollsRoyce, RollsRoyce]
System .
out .
println ( luxuryCars.
size ( ) ) ; //8
//check whether BMW is there in the List if yes then remove it
if ( luxuryCars.contains ( "BMW" ) )
luxuryCars.remove ( "BMW" ) ;
System .
out .
println ( "luxuryCars after removing>>" + luxuryCars
) ; //luxuryCars after removing>>[Audi, MercedesBenz, Porsche, Jaguar, Duesenberg, RollsRoyce, RollsRoyce]
System .
out .
println ( luxuryCars.
size ( ) ) ; //7
//check the list size if it's below 8 and it doesn't contain BMW then add it
if ( luxuryCars.size ( ) < 8 && ! luxuryCars.contains ( "BMW" ) )
luxuryCars.add ( 0 ,"BMW" ) ; // add BMW at starting index
System .
out .
println ( "luxuryCars after adding>>" + luxuryCars
) ; //luxuryCars after adding>>[BMW, Audi, MercedesBenz, Porsche, Jaguar, Duesenberg, RollsRoyce, RollsRoyce]
System .
out .
println ( luxuryCars.
size ( ) ) ; //8 //How to use for-each over List
for ( String car
: luxuryCars
) { }
}
}
cGFja2FnZSBvcmcuSmF2YUluY2xvdWQuamF2YTsKCmltcG9ydCBqYXZhLnV0aWwuQXJyYXlMaXN0OwppbXBvcnQgamF2YS51dGlsLkxpc3Q7CgpwdWJsaWMgY2xhc3MgQ29tbW9uT3BldGF0aW9uV2l0aExpc3QgCnsKCXB1YmxpYyBzdGF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