Note that there are some explanatory texts on larger screens.

plurals
  1. POUsing TreeSet contains() in Java with Integers
    primarykey
    data
    text
    <pre><code>import java.io.*; import java.util.*; class StepfordHouses { private ArrayList&lt;Integer&gt; houses; // A list containing houses private TreeSet&lt;Integer&gt; ordered; // An ordered treeset of houses private TreeSet&lt;Integer&gt; processed; // Elements already processed private String inpline[]; // An array of String holing houses heights in physical order private int disorientedindex; // The index for the Street private int size; // Number of houses in the Street public StepfordHouses() // Constructor for init { houses = new ArrayList&lt;Integer&gt;(); ordered = new TreeSet&lt;Integer&gt;(); processed = new TreeSet&lt;Integer&gt;(); // Basic Input from Text-File (Codechef Requirment) try { BufferedReader br = new BufferedReader(new InputStreamReader( System.in)); size = Integer.parseInt(br.readLine()); inpline = br.readLine().split(" "); } catch (IOException e) { System.out.println("BAAAAAAAAAM!!"); } for (int c = 0; c &lt; size; c++) // Populating Houses { Integer tmp = Integer.parseInt(inpline[c]); houses.add(tmp); ordered.add(tmp); } } public int calcIndex() { int c = 0; while (c &lt; size) { Iterator&lt;Integer&gt; it = ordered.iterator(); Integer h1 = houses.get(c); // Get an element from the raw ArrayList of Houses Integer h = it.next(); // Get an element from the Iterator while (h1.equals(h) != true) { if (processed.contains(h1) == false) { // The element is not already processed System.out.println(h1 + " " + h); disorientedindex++; } h = it.next(); // Get an element from the Iterator } processed.add(h1); c++; it = null; } return disorientedindex; } } public class Work { public static void main(String args[]) { StepfordHouses sh = new StepfordHouses(); System.out.println(sh.calcIndex()); } } </code></pre> <p>The contains() method doesn't work the way I expect it to, i.e compare Integers! The output is 15 , which should be 9 when <code>if(processed.contains(h1)==false)</code> works correctly and returns true when an element is already present!</p> <p>Where could the code be wrong?</p>
    singulars
    1. This table or related slice is empty.
    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.
 

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