We have 2 array,having some element. Comparing both array element at same index, if equal +4 and -1 if not.  Example:  array1=[a b a b], array2=[a a b b]  explanation:           array1[0]==array2[0] //+4          array1[1]!=array2[1] //-1          aarray1[2]==array2[2] //+4          aarray1[3]!=array2[3] //-1                         OUTPUT//6**   My code:  import java.util.Scanner; class  Solution { public static int scoreExam(String[] correct, String[] student) {     int count=0;     for (int i=0;i&lt;correct.length ;i++ )     {         for (int j=0;j&lt;student.length ;j++ )         {             if(i==j)             {                 if(correct[i]==student[j])                 {                     count=count+4;                 }                 else if(correct[i]!=student[j])                 {                     count=count-1;                 }             }         }     }     return (count); }  public static void main(String[] args)  {     Scanner sc=new Scanner(System.in);     String[] array1String,array2String;     String[] array2;     int n=Integer.parseInt(sc.nextLine());     array1String=sc.nextLine().split("//s+");     array2String=sc.nextLine().split("//s+");      array2=new String[array2String.length];     for (int i=0;i&lt;array2String.length ;i++ )     {         if (array2String[i].equals("blank"))         {             array2[i]="";         }         else         {             array2[i]=array2String[i];         }      }      System.out.println(scoreExam(array1String, array2)); } }
93
I've been using the == operator in my program to compare all my strings so far. However, I ran into a bug, changed one of them into .equals() instead, and it fixed the bug.  Is == bad? When should it and should it not be used? What's the difference?
23909
Looking for reviewers to suggest optimization and improvements, and to evaluate coding practices.  final class Variables {     private final int x;     private final int y;      Variables(int x2, int y2) {         this.x = x2;         this.y = y2;     }      public int getX() {         return x;     }      public int getY() {         return y;     }      @Override public String toString()  {          return "x = " + x + " y = " + y;     } }  public final class FindMissing {      private FindMissing() {};      /**  * Takes an input a sorted array with two variables that repeat in the  * range. Remaining variable are consecutive. and returns two variables that repeats  *   * eg: for a range of 1,2,3,4, a valid input is [1, 4], [1, 3] etc.  *   * @param a : the sorted array.  */ /*  * Why is start needed ?  * If start is not given then if a1 is empty how do we know if missing numbers were  * [1, 2] or [4, 5] ??.   */ public static Variables sortedConsecutiveTwoMissing (int[] a, int startOfRange) {     if (a == null) throw new NullPointerException("a1 cannot be null. ");      int low = startOfRange;     // calculating the highest value of the range. it needs to be 2 more than length since all elememts are consecutive.     int high = startOfRange + (a.length - 1) + 2;      int i = 0;     boolean foundX = false;     int x = 0;       while (i &lt; a.length) {         if (a[i] != low) {             if (foundX) {                 return new Variables(x, low);             } else {                 x = low;                 foundX = true;             }         } else {             i++;         }         low++;     }      return new Variables(x, high);  }  public static void main(String[] args) {         // unsortedConsecutiveTwoMissing         int[] a6 = {1, 4};         System.out.println("Expected 2 and 3, Actual " + sortedConsecutiveTwoMissing(a6, 1));         // unsortedConsecutiveTwoMissing         int[] a7 = {-4, -1};         System.out.println("Expected -3 and -2, Actual " + sortedConsecutiveTwoMissing(a7, -4));         int[] a8 = {-4, -3, -2, -1};         System.out.println("Expected 0 and 1, Actual " + sortedConsecutiveTwoMissing(a8, -4));         int[] a9 = {5};         System.out.println("Expected 4 and 6, Actual " + sortedConsecutiveTwoMissing(a9, 4));         }      }
1992
While working in a Java app, I recently needed to assemble a comma-delimited list of values to pass to another web service without knowing how many elements there would be in advance. The best I could come up with off the top of my head was something like this:  public String appendWithDelimiter( String original, String addition, String delimiter ) {     if ( original.equals( "" ) ) {         return addition;     } else {         return original + delimiter + addition;     } }  String parameterString = ""; if ( condition ) parameterString = appendWithDelimiter( parameterString, "elementName", "," ); if ( anotherCondition ) parameterString = appendWithDelimiter( parameterString, "anotherElementName", "," );   I realize this isn't particularly efficient, since there are strings being created all over the place, but I was going for clarity more than optimization.  In Ruby, I can do something like this instead, which feels much more elegant:  parameterArray = []; parameterArray &lt;&lt; "elementName" if condition; parameterArray &lt;&lt; "anotherElementName" if anotherCondition; parameterString = parameterArray.join(",");   But since Java lacks a join command, I couldn't figure out anything equivalent.  So, what's the best way to do this in Java?
8867
When should I use -eq vs = vs ==  e.g.    [[ $num -eq 0 ]]  [[ $num = 'zzz' ]]   I've observed a pattern of using -eq (and -ne, etc.) for numbers and = for strings.  Is there a reason for this and when should I use ==
106126
I was debugging something and discovered some strangeness in JavaScript:  alert(1=='') ==&gt; false alert(0=='') ==&gt; true alert(-1=='') ==&gt; false   It would make sense that an implied string comparison that 0 should = '0'. This is true for all non-zero values, but why not for zero?
56415
I have four foreach loops that iterate through the collections and based on a condition do something.   Here is the code that I am writing now:  boolean breakFlag = false; String valueFromObj2 = null; String valueFromObj4 = null; for(Object1 object1: objects){   for(Object2 object2: object1){     // I get some value from object2     valueFromObj2 = object2.getSomeValue();     for(Object3 object3 : object2){       for(Object4 object4: object3){        // Finally I get some value from Object4.        valueFromObj4 = object4.getSomeValue();        // Compare with valueFromObj2 to decide either to break all the foreach loop        breakFlag = compareTwoVariable(valueFromObj2, valueFromObj4 );        if(breakFlag){break;}       } // fourth loop ends here       if(breakFlag){break;}     } // third loop ends here     if(breakFlag){break;}   } // second loop ends here   if(breakFlag){break;} } // first loop ends here   The main object (objects in the code) comes from a third-party provider SDK, so I cannot change anything on that portion. I want to ask the Stack&nbsp;Overflow community if there is a better approach to break all the four foreach loops. Or if there is any other way to refactor this code to make it more readable and maintainable.
13369
I have an Object[] array, and I am trying to find the ones that are primitives. I've tried to use Class.isPrimitive(), but it seems I'm doing something wrong:  int i = 3; Object o = i;  System.out.println(o.getClass().getName() + ", " +                    o.getClass().isPrimitive());   prints java.lang.Integer, false.  Is there a right way or some alternative?
95859
Why does C/C++ differentiates in case of array index out of bound #include &lt;stdio.h&gt; int main() {     int a[10];     a[3]=4;     a[11]=3;//does not give segmentation fault     a[25]=4;//does not give segmentation fault     a[20000]=3; //gives segmentation fault     return 0; }  I understand that it's trying to access memory allocated to process or thread in case of a[11] or a[25] and it's going out of stack bounds in case of a[20000]. Why doesn't compiler or linker give an error, aren't they aware of the array size? If not then how does sizeof(a) work correctly?
40162
Please demonstrate how the ternary operator works with a regular if/else block. Example:  Boolean isValueBig = value &gt; 100 ? true : false;     Exact Duplicate:
67149
I'm trying to create an array of arrays that will be using repeated data, something like below:  int[] list1 = new int[4] { 1, 2, 3, 4 }; int[] list2 = new int[4] { 5, 6, 7, 8 }; int[] list3 = new int[4] { 1, 3, 2, 1 }; int[] list4 = new int[4] { 5, 4, 3, 2 };  int[,] lists = new int[4, 4] {  list1 ,  list2 ,  list3 ,  list4  };   I can't get it to work and so I'm wondering if I'm approaching this wrong.   What I'm attempting to do is create some sort of method to create a long list of the values so I can process them in a specific order, repeatedly. Something like,  int[,] lists = new int[90,4] { list1, list1, list3, list1, list2, (and so on)};  for (int i = 0; i &lt; 90; ++i) {    doStuff(lists[i]); }   and have the arrays passed to doStuff() in order. Am I going about this entirely wrong, or am I missing something for creating the array of arrays?
75403
I have an array:  data.Dealer.car[0] data.Dealer.car[1] data.Dealer.car[2]   If I do this:  alert(data.Dealer.car.length); delete data.Dealer.car[1]; alert(data.Dealer.car.length);   It gives me the same count each time. Does the removed element still exist?
6134
I had a task where I had to figure if the argument was true or not.  $A=\{ n ∈ ℤ \mid n^2 &lt; 5 \}, \quad  B=\{ 7, 8, \{2\}, \{2, 7, 8\}, \{\{7\}\} \}$  The first one was $\{-1, 2\} ∈ A$ and the answer to this was not true  since the set is not an integer.  The second one was $\{-1, 2\} ⊂ A$ and the answer to this was true since  -1 and 2 are elements of $A$.  I just can't seem to understand the difference between ∈ and ⊂ in this task and why the first argument was not true. Also it confuses me that the answer to the first one was that not true because the set is not an integer but aren't -1 and 2 integers?  There is other parts of this task that also add more confusion:     {2, 7, 8} ∈ B Answer: True since {2, 7, 8} is an element of set B      {2, 7, 8} ⊂ B Answer: Not true, for example 2 is not an element in set   B
81401
I have an array of doubles and I want the index of the highest value. These are the solutions that I've come up with so far but I think that there must be a more elegant solution. Ideas?  double[] score = new double[] { 12.2, 13.3, 5, 17.2, 2.2, 4.5 }; int topScoreIndex = score.Select((item, indx) =&gt; new {Item = item, Index = indx}).OrderByDescending(x =&gt; x.Item).Select(x =&gt; x.Index).First();  topScoreIndex = score.Select((item, indx) =&gt; new {Item = item, Index = indx}).OrderBy(x =&gt; x.Item).Select(x =&gt; x.Index).Last();  double maxVal = score.Max(); topScoreIndex = score.Select((item, indx) =&gt; new {Item = item, Index = indx}).Where(x =&gt; x.Item == maxVal).Select(x =&gt; x.Index).Single();
123088
I am writing a SQL query creator using some parameters. In Java, it's very easy to detect the last element of an array from inside the for loop by just checking the current array position with the array length.  for(int i=0; i&lt; arr.length;i++){      boolean isLastElem = i== (arr.length -1) ? true : false;         }   In PHP they have non-integer indexes to access arrays. So you must iterate over an array using a foreach loop. This becomes problematic when you need to take some decision (in my case to append or/and parameter while building query).   I am sure there must be some standard way of doing this.   How do you solve this in PHP?
7374
I'm designing an algorithm to do the following: Given array A[1... n], for every i &lt; j, find all inversion pairs such that A[i] &gt; A[j]. I'm using merge sort and copying array A to array B and then comparing the two arrays, but I'm having a difficult time seeing how I can use this to find the number of inversions. Any hints or help would be greatly appreciated.
11629
Is it possible to delete multiple elements from a list at the same time? If I want to delete elements at index 0 and 2, and try something like del somelist[0], followed by del somelist[2], the second statement will actually delete somelist[3].  I suppose I could always delete the higher numbered elements first but I'm hoping there is a better way.
62295
Is there in the JDK or Jakarta Commons (or anywhere else) a method that can parse the output of Arrays.toString, at least for integer arrays?  int[] i = fromString(Arrays.toString(new int[] { 1, 2, 3} );