In my Class I declared an Array  myUsers = new Array();   Where I want to Add Usernames, unique. So i thought I would check if the ArrayLenght is 0, if yes, add a User. Which works fine. I if a Username wants to be added, which exists in the Array, it should be deleted. But this way, If I add one User the console says ["User1"] If I try to add another one (another one User2) the console says ["User1", empty] Can't get behind that...  isChecked(e) {      var usersArray = this.myUsers;      console.log(usersArray.length);    if(this.myUsers.length == 0) {       console.log("is 0");       this.myUsers.push(e);     } else {       console.log("is not 0");       for (var i= 0; i &lt; this.myUsers.length; i++) {         if (this.myUsers[i] == e) {            console.log("is in array");            delete this.myUsers[i];           } else {             this.myUsers.push(e);           }      }   }   }
5654
What is the difference between using  on the array element as opposed to using ?   For example:  myArray = ['a', 'b', 'c', 'd'];  delete myArray[1]; //  or myArray.splice (1, 1);   Why even have the splice method if I can delete array elements like I can with objects?
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));         }      }
9942
Is there an easy way to delete an element from an array using PHP, such that foreach ($array) no longer includes that element?  I thought that setting it to null would do it, but apparently it does not work.
9790
If you have a statically allocated array, the Visual Studio debugger can easily display all of the array elements.  However, if you have an array allocated dynamically and pointed to by a pointer, it will only display the first element of the array when you click the + to expand it.  Is there an easy way to tell the debugger, show me this data as an array of type Foo and size X?
35414
In javascript you can easily create objects and Arrays like so:  var aObject = { foo:'bla', bar:2 }; var anArray = ['foo', 'bar', 2];   Are similar things possible in PHP? I know that you can easily create an array using the array function, that hardly is more work then the javascript syntax, but is there a similar syntax for creating objects? Or should I just use associative arrays?  $anArray = array('foo', 'bar', 2); $anObjectLikeAssociativeArray = array('foo'=&gt;'bla',                                       'bar'=&gt;2);   So to summarize: Does PHP have javascript like object creation or should I just use associative arrays?
104197
When i create new User Account and login with that credentials, it gives me this problem if admin doesnt have set thier values yet !!!**  AllotmentList = [select id, Days_Remaining__c from PTO_Allotment__c where Year__c =:year and Employee__c=:EmployeeList[0].id LIMIT 100]; //access only Current user Allotment details                 //to handle Listoutof Bound Exception wrote if-else          if(AllotmentList.size()&gt;0)         {         requests.PTO_Allotment__c=AllotmentList[0].id;         requests.Days_Remaining__c=AllotmentList[0].Days_Remaining__c;         }         else         {             //write a code when AllotmentList is empty ....         }
73249
Is there any method for creating a dynamic array in C#?
23917
Which is the right thing to do?  if (myObj['key'] == undefined)   or  if (myObj['key'] == null)   or  if (myObj['key'])
1085
Does the "for…in" loop in Javascript loop through the hashtables/elements in the order they are declared? Is there a browser which doesn't do it in order? The object I wish to use will be declared once and will never be modified.  Suppose I have:  var myObject = { A: "Hello", B: "World" };   And I further use them in:  for (var item in myObject) alert(item + " : " + myObject[item]);   Can I expect 'A : "Hello"' to always come before 'B : "World"' in most decent browsers?
98955
Suppose I have this code: var myArray = new Object(); myArray[&quot;firstname&quot;] = &quot;Bob&quot;; myArray[&quot;lastname&quot;] = &quot;Smith&quot;; myArray[&quot;age&quot;] = 25;  Now if I wanted to remove &quot;lastname&quot;?....is there some equivalent of myArray[&quot;lastname&quot;].remove()? (I need the element gone because the number of elements is important and I want to keep things clean.)
10572
Given an array of n Objects, let's say it is an array of strings, and it has the following values:  foo[0] = "a"; foo[1] = "cc"; foo[2] = "a"; foo[3] = "dd";   What do I have to do to delete/remove all the strings/objects equal to "a" in the array?
40938
After I posted this question, I found a similar post that seemed to answer it: . I'll leave this in place for now, as I am not sure how to mark my own question as a duplicate  In attempting to debug a SOQL query error, I encountered a strange behavior with Database.query and bind variables. Specifically, when I run this code:  String userQuery = 'SELECT Id FROM User'; if (true) {     List&lt;String&gt; firstNames = new List&lt;String&gt;{'James','Joe'};     userQuery += ' WHERE FirstName IN :firstNames'; } Database.query(userQuery);   the Database.query statement executes with no errors, despite the fact that the variable firstNames does not exist in its context. However, when I add an else clause:  String userQuery = 'SELECT Id FROM User'; if (true) {     List&lt;String&gt; firstNames = new List&lt;String&gt;{'James','Joe'};     userQuery += ' WHERE FirstName IN :firstNames'; } else {     List&lt;String&gt; firstNames; } Database.query(userQuery);   I receive a null object reference exception. It seems like Salesforce is checking to see if the variable is only declared once beforehand and, if so, uses its value in the context it was declared. This seems strange to me, so I was wondering if anybody here has any insight into what is going on under the hood.
106280
Object o = new Long[0] System.out.println( o.getClass().isArray() ) System.out.println( o.getClass().getName() ) Class ofArray = ???   Running the first 3 lines emits;  true [Ljava.lang.Long;   How do I get ??? to be type long?   I could parse the string and do a Class.forname(), but thats grotty.   What's the easy way?
78724
This code in JS gives me a popup saying "i think null is a number", which I find slightly disturbing. What am I missing?    if (isNaN(null)) {   alert("null is not a number"); } else {   alert("i think null is a number"); }     I'm using Firefox 3. Is that a browser bug?  Other tests:    console.log(null == NaN);   // false console.log(isNaN("text")); // true console.log(NaN == "text"); // false     So, the problem seems not to be an exact comparison with NaN?  Edit: Now the question has been answered, I have cleaned up my post to have a better version for the archive. However, this renders some comments and even some answers a little incomprehensible. Don't blame their authors. Among the things I changed was:   Removed a note saying that I had screwed up the headline in the first place by reverting its meaning Earlier answers showed that I didn't state clearly enough why I thought the behaviour was weird, so I added the examples that check a string and do a manual comparison.
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?
832
.NET offers a generic list container whose performance is almost identical (see Performance of Arrays vs. Lists question). However they are quite different in initialization.   Arrays are very easy to initialize with a default value, and by definition they already have certain size:  string[] Ar = new string[10];   Which allows one to safely assign random items, say:  Ar[5]="hello";   with list things are more tricky. I can see two ways of doing the same initialization, neither of which is what you would call elegant:  List&lt;string&gt; L = new List&lt;string&gt;(10); for (int i=0;i&lt;10;i++) L.Add(null);   or  string[] Ar = new string[10]; List&lt;string&gt; L = new List&lt;string&gt;(Ar);   What would be a cleaner way?  EDIT: The answers so far refer to capacity, which is something else than pre-populating a list. For example, on a list just created with a capacity of 10, one cannot do L[2]="somevalue"  EDIT 2: People wonder why I want to use lists this way, as it is not the way they are intended to be used. I can see two reasons:   One could quite convincingly argue that lists are the "next generation" arrays, adding flexibility with almost no penalty. Therefore one should use them by default. I'm pointing out they might not be as easy to initialize. What I'm currently writing is a base class offering default functionality as part of a bigger framework. In the default functionality I offer, the size of the List is known in advanced and therefore I could have used an array. However, I want to offer any base class the chance to dynamically extend it and therefore I opt for a list.
84232
Alright, I think we all agree that what happens with the following code is undefined, depending on what is passed,  void deleteForMe(int* pointer) {      delete[] pointer; }   The pointer could be all sorts of different things, and so performing an unconditional delete[] on it is undefined. However, let's assume that we are indeed passing an array pointer,  int main() {      int* arr = new int[5];      deleteForMe(arr);      return 0; }   My question is, in this case where the pointer is an array, who is it that knows this? I mean, from the language/compiler's point of view, it has no idea whether or not arr is an array pointer versus a pointer to a single int. Heck, it doesn't even know whether arr was dynamically created. Yet, if I do the following instead,  int main() {      int* num = new int(1);      deleteForMe(num);      return 0; }   The OS is smart enough to only delete one int and not go on some type of 'killing spree' by deleting the rest of the memory beyond that point (contrast that with strlen and a non-\0-terminated string -- it will keep going until it hits 0).  So whose job is it to remember these things? Does the OS keep some type of record in the background? (I mean, I realise that I started this post by saying that what happens is undefined, but the fact is, the 'killing spree' scenario doesn't happen, so therefore in the practical world someone is remembering.)