Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>As Brian said, Strings in java are immutable. This means that you can't assign through a method call like <code>m2.charAt(j)=' '</code>.This means you have to use another way to keep track of whether you've found the character yet. </p> <p>You could add it to <code>intersection</code> and when checking a character make sure it isn't in <code>intersection</code> by using <code>intersection.indexOf(char c)</code>, if this returns -1 then it isn't in the string. </p> <p>edit:</p> <p>Sorry didn't put into account that the output should be a multiset. The above solves the problem if the output is a set. </p> <p>You could use <code>replaceFirst(String searchFor, String replacement)</code> on your m2 to delete it. it would be something like:</p> <pre><code> for( int i =0; i &lt; m1.length(); i+=2) { if(m2.indexOf(m1.charAt(i)) != -1) { intersection = intersection + m1.charAt(1) + " "; m2 = m2.replaceFirst(new String(m1.charAt(i)), ""); } } </code></pre> <p>So if m1 = '1 1 2 3 5' and m2 = '1 4 2 1',</p> <p>first pass: looks for 1 in '1 4 2 1'</p> <p>second pass: looks for 1 in '4 2 1'</p> <p>third pass: looks for 2 in '4 2'</p> <p>fourth pass: looks for 3 in '4'</p> <p>fifth pass: looks for 5 in '4'</p> <p>returning '1 1 2'</p> <p>Note that that it is incrementing the variable by two to take into account spaces. This is <strong>only</strong> if we assume that the two strings are in the form 'a a a a a a', with 'a' only being a single character. If there are digits or characters that are more than a digit long then you have to skip whitespace and interpret the string in a different way, other than just looking at it at a character-by-character basis.</p> <p>If we can make these assumtions, it would be wise to trim your m1 of trailing and leading whitespace using the String <code>trim</code> method ie <code>m1 = m1.trim()</code> before executing this loop. </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.
    1. VO
      singulars
      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