65 767
Assignments Done
99,4%
Successfully Done
In October 2018

Answer to Question #19881 in Java | JSP | JSF for Sayed Abdullah Almousawi

Question #19881
write a Java method to delete the last occurrence of the integer that was chosen by the user from the array and then prints the updated contents of the array.
Expert's answer
public static void main(String[] args)
{
int[]a=new int[5];
gen(a);

for(int i = 0; i < a.length; i++)
{
System.out.print(a[i]+"");
}

Scanners=new Scanner(System.in);

System.out.println("
enterindex of element to delete");

intind;
while(s.hasNextInt())
{
if((ind=s.nextInt())<a.length)
{
a=delete(ind,a);
}
else
{
System.out.println("indexerror");
}
}
}

/**
* fill array with random values
* @param a
*/
publicstatic void gen(int[] a)
{
for(inti=0;i<a.length;i++)
{
a[i]=(newRandom()).nextInt(100);
}
}
/**
* delete element from array with index i
* @param i
* @return
*/
publicstatic int[] delete(int i, int a[])
{
int[]arr=new int[a.length-1];
intcnt=0;

for(int j = 0; j < a.length; j++)
{
if(j!=i)
{
arr[cnt]=a[j];
cnt++;
}
}

//printsupdated array
for(int j = 0; j < arr.length; j++)
{
System.out.print(arr[j]+"");
}
System.out.println();

returnarr;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be first!

Leave a comment

Ask Your question

Submit
Privacy policy Terms and Conditions