Note that there are some explanatory texts on larger screens.

plurals
  1. POArray trouble deleting duplicates
    primarykey
    data
    text
    <p>This is for class homework. She is making us use array and array list in two parts. Basically she is showing us how using arraylist is alot easier than arrays.</p> <p>I am having alot of trouble getting the array part to work.</p> <blockquote> <ol> <li><p>Create a class called CustomerLister1 with a main method that instantiates an array of String objects called customerName. The array should have room for six String objects. Use an initializer list to put the following names into the array:</p> <pre><code>Chris Lois Meg Peter Stewie </code></pre></li> <li><p>Write an enhanced for loop to display the array of names. What is displayed for the last array element? Why is it that value?</p></li> <li><p>Add the Strings "Meg" and "Brian" into index 3, and 4, respectively, so that the array contains the following elements:</p> <pre><code>Chris Lois Meg Meg Brian Peter Stewie </code></pre></li> <li><p>Write an enhanced for loop to display the array of names.</p></li> <li><p>Write a second, traditional for loop that checks each element for the String “Meg”, if found in the array, remove it, shift the remaining elements, and display the array of names. Are both instances of "Meg" removed correctly from the array?</p></li> </ol> </blockquote> <p>This is my code </p> <pre><code>public class CustomerLister1 { public static void main(String[] args) { String[] customerName = new String[7]; customerName[0] = "Chris"; customerName[1] = "Lois"; customerName[2] = "Meg"; customerName[3] = "Peter"; customerName[4] = "Stewie"; for (int i = customerName.length-1;i &gt; 3; i--) { customerName[i] = customerName[i - 2]; } customerName[3] = "Meg"; customerName[4] = "Brian"; for (int m = 0; m &lt;= customerName.length-1; m++) { if(customerName[m].equals("Meg")) { for(int j = m+1;j &lt; customerName.length-1;j++) { customerName[j]= customerName[j-1]; } } } for (String element : customerName) { System.out.println(element); } } } </code></pre> <p>I have tried messing with the element position. It won't print right because I have meg back to back and it replaces the first meg with the second meg. What can i do here?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload