In: Computer Science
Please write in JAVA
1. Given the following array-based ADT list called colorList
whose elements contain strings
red, orange, yellow, blue, indigo, violet
write the statement to insert the String element “pink” to the end
of the list. Assume the front of the list is on the left.
2. Outline the basic steps to remove a node from the beginning of a list.
Completed answers will be given an immediate upvote :)
Code using inbuilt arraylist (just copy and paste to see the output)
import java.util.*;//util class contains arraylist
public class HelloWorld{
public static void main(String
[]args){
ArrayList<String>
colourlist=new ArrayList<String>();//creating a arraylist of
type string with name as colourlist
colourlist.add("red");
colourlist.add("orange");
colourlist.add("yellow");
colourlist.add("blue");
colourlist.add("indigo");
colourlist.add("violet");
System.out.println("creating the colourlist with predfined and now
outputing each element");
for(int
i=0;i<colourlist.size();i++){
System.out.println(colourlist.get(i));
}
//actual code given from
here
//adding pink to end of
list
colourlist.add("pink");//add basically adds element to end of
arraylist adt
System.out.println("displaying the colourlist adding the element
pink to end of list");
for(int
i=0;i<colourlist.size();i++){
System.out.println(colourlist.get(i));
}
//NOW I AM REMOVING THE
FIRST ELEMENT FROM LIST
colourlist.remove(0);//remove o means remioving first element from
list
System.out.println("colourlist after removing the first element ie
red");
for(int
i=0;i<colourlist.size();i++){
System.out.println(colourlist.get(i));
}
System.outprintln("in
arraylist elements are deleted and inserted as deleted or inserted
in simple array this process is carried by compiler itself");
}
}//end of class