In: Computer Science
We have created an ArrayList of Person class. write a method called push that pushes all the people with the even length last name to the end of the ArrayList
Content of the ArrayList before push
[alex Bus, Mary Phillips, Nik Lambard, Rose Rodd, Esa khan, Jose Martinex, Nik Patte]
content of the ArrayList after the push method
[alex Bus, Nik Lambard, Nik Patte, Mary Phillips, Rose Rodd, Esa
khan, Jose Martinex]
import java.util.*;
class Person
{
private String name;
private String last;
public Person(String name, String last)
{
this.name = name;
this.last = last;
}
public String getLast()
{
return last;
}
public String getFirst()
{
return name;
}
public String toString()
{
return name + " " + last;
}
}
public class ArrayList
{
public static void main(String[] args)
{
ArrayList<Person> list = new ArrayList<Person>();
list.add(new Person ("alex","Bus"));
list.add(new Person("Mary", "Phillips"));
list.add(new Person("Nik", "Lambard") );
list.add(new Person("Rose","Rodd"));
list.add(new Person("Esa","khan"));
list.add(new Person("Jose","Martinex"));
list.add(new Person("Nik","Patte"));
System.out.println(list);
push(list);
System.out.println(list);
}
//this method pushes all the people with the even length last name to the end of the list
public static void push(ArrayList<Person> list) {
}
}
import java.util.ArrayList;
import java.util.List;
class Person {
private String name;
private String last;
public Person(String name, String last) {
this.name = name;
this.last = last;
}
public String getLast() {
return last;
}
public String getFirst() {
return name;
}
public String toString() {
return name + " " + last;
}
}
public class ArrayListDemo {
public static void main(String[] args) {
ArrayList<Person> list = new ArrayList<Person>();
list.add(new Person("alex", "Bus"));
list.add(new Person("Mary", "Phillips"));
list.add(new Person("Nik", "Lambard"));
list.add(new Person("Rose", "Rodd"));
list.add(new Person("Esa", "khan"));
list.add(new Person("Jose", "Martinex"));
list.add(new Person("Nik", "Patte"));
System.out.println(list);
push(list);
System.out.println(list);
}
//this method pushes all the people with the even length last name to the end of the list
public static void push(ArrayList<Person> list) {
int size=list.size();
for (int i=0;i<size;i++) {
if (list.get(i).getLast().length() % 2 == 0) {
Person p=list.remove(i);
list.add(p);
}
}
}
}
NOTE : PLEASE COMMENT BELOW IF YOU HAVE CONCERNS.
I AM HERE TO HELP YOUIF YOU LIKE MY ANSWER PLEASE RATE AND HELP ME IT IS VERY IMP FOR ME