Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>I read your <a href="https://stackoverflow.com/questions/8637815/naming-conventions-in-java">other post</a> and got the impression you are a new java developer, so I found the need to give a running example. Simply copy paste this to your IDE and run it. It should run and produce an output which is a proof of concept for you. </p> <p>The input I am using here is </p> <pre><code>String[] input = new String[]{ "1,2,1" , "1,1,2", "1,2,3", "1,3,1", "1,1,1", "1,3,3", "1,1,3"}; </code></pre> <p>And as you can see it has repetitions as you described. The output is </p> <pre><code> The triangle [Triangle{sides=[1, 1, 2]}] already exists! The triangle [Triangle{sides=[1, 1, 3]}] already exists! triangles = [Triangle{sides=[1, 2, 3]}, Triangle{sides=[1, 1, 3]}, Triangle{sides=[1, 1, 2]}, Triangle{sides=[1, 1, 1]}, Triangle{sides=[1, 3, 3]}] </code></pre> <p>As you requested the output is with no repetition. </p> <pre><code>import java.io.BufferedReader; import java.io.File; import java.io.FileReader; import java.io.IOException; import java.util.Arrays; import java.util.HashSet; import java.util.Set; public class Triangle { private int[]sides; public Triangle( int x, int y, int z) { sides = new int[]{x,y,z}; Arrays.sort( sides ); } public Triangle( String ... args ) { this( Integer.parseInt( args[0].trim() ), Integer.parseInt( args[1].trim() ), Integer.parseInt( args[2].trim() )); } @Override public boolean equals( Object o ) { if ( this == o ) return true; if ( o == null || getClass() != o.getClass() ) return false; Triangle triangle = ( Triangle ) o; if ( !Arrays.equals( sides, triangle.sides ) ) return false; return true; } @Override public int hashCode() { return sides != null ? Arrays.hashCode( sides ) : 0; } @Override public String toString() { return "Triangle{" + "sides=" + Arrays.toString( sides ) + '}'; } public static void main( String[] args ) throws IOException { String[] input = new String[]{ "1,2,1" , "1,1,2", "1,2,3","1,3,1","1,1,1","1,3,3","1,1,3"}; Set&lt;Triangle&gt; triangles = new HashSet&lt;Triangle&gt;( ); for ( String s : input ) { Triangle triangle = new Triangle( s.split( "," ) ); if ( triangles.contains( triangle )) { System.out.println("The triangle [" + triangle + "] already exists!"); } triangles.add( triangle ); } System.out.println( "triangles = " + triangles ); } } </code></pre>
 

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