[Solved] algorithm removing duplicate elements in array without auxillay storage

OK, here’s my answer, which should be O(N*N) worst case. (With smaller constant, since even worstcase I’m testing N against — on average — 1/2 N, but this is computer science rather than software engineering and a mere 2X speedup isn’t significant. Thanks to @Alexandru for pointing that out.) 1) Split cursor (input and output … Read more

[Solved] Regex pattern for split

This regex does the trick: “,(?=(([^\”]*\”){2})*[^\”]*$)(?=([^\\[]*?\\[[^\\]]*\\][^\\[\\]]*?)*$)” It works by adding a look-ahead for matching pairs of square brackets after the comma – if you’re inside a square-bracketed term, of course you won’t have balanced brackets following. Here’s some test code: String line = “a=1,b=\”1,2,3\”,c=[d=1,e=\”1,11\”]”; String[] tokens = line.split(“,(?=(([^\”]*\”){2})*[^\”]*$)(?=([^\\[]*?\\[[^\\]]*\\][^\\[\\]]*?)*$)”); for (String t : tokens) System.out.println(t); Output: … Read more

[Solved] How to make Java Set? [closed]

Like this: import java.util.*; Set<Integer> a = new HashSet<Integer>(); a.add( 1); a.add( 2); a.add( 3); Or adding from an Array/ or multiple literals; wrap to a list, first. Integer[] array = new Integer[]{ 1, 4, 5}; Set<Integer> b = new HashSet<Integer>(); b.addAll( Arrays.asList( b)); // from an array variable b.addAll( Arrays.asList( 8, 9, 10)); // … Read more

[Solved] Optimal way to creating a multiplication table -java

You can define the table size and print the multiplication grid as follows: public static void main(String[]args) { final int TABLE_SIZE = 12; // Declare the rectangular array to store the multiplication table: int[][] table = new int[TABLE_SIZE][TABLE_SIZE]; // Fill in the array with the multiplication table: for(int i = 0 ; i < table.length … Read more

[Solved] How Java String and StringBuffer works

In your case s1 is a String and s2 is StringBuffer. The Object class’s equals method returns true if this instance is equal to the argument. That is why you got s1.equals(s2) as false. But If you are converting the s2 to a String then that will give true value. Because string builder’s toString() method … Read more

[Solved] =- operator in java

— is a “decrement” operator in Java and many other languages. The reason the compiler doesn’t treat it as two – operators is that there’s a basic rule that the compiler will look for the longest sequence of consecutive characters that forms one of its “separators” or “operators”, as defined here. (> characters are handled … Read more

[Solved] can not resolve symbol ‘from’ Error in Android Studio

You’re calling LayoutInflater.from as if you were calling a constructor: LayoutInflater inflater = new LayoutInflater.from(parent.getContext()); That’s a mixture of constructor-call syntax (new) and method call syntax (.from). It would work if LayoutInflater.from were a static nested class, but it’s not… it’s just at static method. So you want: LayoutInflater inflater = LayoutInflater.from(parent.getContext()); solved can not … Read more

[Solved] new instance of the class in java main method

In java a class has fields and methods (or functions). Keyword ‘static’ can be added to both of these entities. Entities marked with keyword ‘static’ are class related and other entities are instance related. For accessing static fields or methods of a class we require only the class and its instance (created by using new … Read more

[Solved] In the below java program I didn’t understand flow of execution and about “this” keyword execution?

Your constructor prints one line, so : new This(); System.out.println(new This()); Here you call the constructor twice, and System.out.println() also prints a line, so you get three lines. Also, you are creating two distinct This objects. In your second example, the constructor is only called once, so you get only two lines. This example creates … Read more