dt View Go Tools Window Help D Arrays Lab.pdf (1 page)t\". Edited Q Search Array
ID: 3592727 • Letter: D
Question
Explanation / Answer
The Required code is as follows:
import java.io.*;
import java.util.*;
import java.lang.*;
class CustomerListerArray {
public static void main (String[] args) {
//System.out.println("GfG!");
String ar[] = new String[7];
ar[0] = "Beth";
ar[1] = "Jerry";
ar[2] = "Rick";
ar[3] = "Summer";
ar[4] = "Morty";
for(int i=0;i<7;i++)
{
System.out.println(ar[i]);
}
System.out.println();
ar[3] = "Jerry";
ar[4] = "Jessica";
ar[5] = "Summer";
ar[6] = "Morty";
for(int i=0;i<7;i++)
{
System.out.println(ar[i]);
}
System.out.println();
String names[] = new String[7];
int j=0;
for(int i=0;i<7;i++)
{
if(ar[i] == "Jerry")
{
names[j++] = ar[i+1];
i++;
}
else
{
names[j++] = ar[i];
}
}
for(int i=0;i<7;i++)
{
System.out.println(names[i]);
}
System.out.println();
}
}
The output is shown below:
Beth
Jerry
Rick
Summer
Morty
null
null
Beth
Jerry
Rick
Jerry
Jessica
Summer
Morty
Beth
Rick
Jessica
Summer
Morty
null
null
The answer to the questions are:
1. Null is displayed for last two elements.
2. This is because, when an array is initialised with a given size, all the elements are equated to null by default.
3. Yes, both the instances of "Jerry have been removed".
4. To delete an element from a given array, another array is required to copy and shift the value of array elements to the required position.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.