Java Array deleting confusion? -


I am trying to create a program that wants the user to remove an element from the array, and a loop Removing in I have a problem that is removing the extra array elements after removing an element. I was going to this for about 3 hours and no one found Kripa Meri to help. Thank you.

What do I mean here:

  array list: 10 20 30 40 50 Which number should be removed for the list? Number to delete: 20 10 30 40 50 Try again? Which number should be removed for the Y / N list? Number to delete: 30 10 40 ** 50 50 ** Try again? Y / n  

What do I have here:

  import java.util.Scanner; Public category DeleteElements {public static zero main (string [] args) {scanner input = new scanner (System.in); Int del = 0; Int intArray [] = {10, 20, 30, 40, 50}; System.out.println ("\ n ---- Part 3 ----" of homework); Four yen; Boolean Yes = True; For (int i = 0; i & lt; intArray.length; i ++) {System.out.print (intArray [i] + ""); } {System.out.print ("\ n Which number should be deleted for the list?"); Del = input.nextInt (); System.out.println ("Number of deletions:" + del); For (int i = 0; i & lt; intArray.length; i ++) {if (intArray [i] == del) {if (i == 0) {for (int b = i; b & lt ; IntArray.length - 1; B ++) {intare [b] = intere [b + 1]; System.out.print (intArray [b] + ""); }} If {i & gt; 0 & amp; I & lt; = IntArray [i]) {for (int c = 0; c & lt; i; c ++) {System.out.print (intArray [c] + ""); } For (int a = i; a & lt; intArray.length - 1; a ++) {intArray [a] = intArray [a + 1]; System.out.print (intArray [a] + ""); }}}} System.out.println ("\ nReturn? Y / N"); Input.nextLine (); YN = input.nextLine (). Fourat (0); If (yN == 'y') {yes = true; } And if (yN == 'n') {yes = false; }} While (yes! = Wrong); }}  

The problem is in the code that prints the result: it always assumes that Only one item is "deleted". Of course you can not actually remove items from an array - what you can do is show that the last of elements are not there, and do not print them .

Instead of

  (int a = i; a  
-> You should type removedCount
  for  

(int a = i; a) & Lt; intArray.length - removedCount; a ++) / code> at zero, and each time you remove an element, it increases it.


Comments

Popular posts from this blog

import - Python ImportError: No module named wmi -

Editing Python Class in Shell and SQLAlchemy -

c# - MySQL Parameterized Select Query joining tables issue -