can not determinate index in array

get index of element in array javascript
array index
find all index of element in array javascript
javascript indexof object
array find
indexof undefined

Strings are added to the array, to determine whether the list is ordered by increasing the length of the string. If not, print the index of the first element that violates such ordering. Everything works correctly if the strings in the array are different, for example, enter


Answer: index (wa) 3 output.

but if it will be like this:


Answer: index (123) - 0 but the correct answer is index 2

public class Solution {

    public static void main(String[] args) throws IOException {

             Scanner scan = new Scanner(;
             ArrayList<String> list = new ArrayList<>();
        for (int i = 0; i < 4; i++) {
        int count = 0;
        for(int i = 0; i < list.size(); i++){
            if(count+2 > list.size()) {
            if(list.get(i+1).length() <= list.get(i).length()){
                       count = count + 1;

You should change




since if there are multiple occurrences of the same String in the List, you want to return the index of the first element that violates the ordering, which is i+1 (and not the index of the first String which is equal to that element).

BTW, even if there were no duplicate elements in your List, it would no sense to use list.indexOf(list.get(i+1)) instead of simply i+1.

Array.prototype.findIndex(), The findIndex() method returns the index of the first element in the Otherwise, it returns -1, indicating that no element passed the test. If callback never returns a truthy value (or the array's length is 0 ), findIndex() returns -1 . Likewise, an unfixed indeterminate array inherits the dimension of an indeterminate array parameter fixed using the EXTENT statement in the called routine. You cannot pass an unfixed indeterminate array to a routine whose corresponding parameter is a determinate array defined as an INPUT parameter.

You don't need to use lastIndexOf - you already have the index:

// Stsrt at index 1, as index 0 can never violate the rule:
for (int i = 1; i < list.size(); i++) {
    if (list.get(i).length() < list.get(i - 1).length() {
        System.out.println("Rule violated at index " + i + " (" + list.get(i) + ")");

Array.prototype.filter(), let newArray = arr .filter( callback ( element [, index , [ array ]])[, thisArg ]) Array elements which do not pass the callback test are simply� Searches for an element that matches the conditions defined by the specified predicate, and returns the zero-based index of the first occurrence within the range of elements in the Array that extends from the specified index to the last element. FindIndex<T>(T[], Int32, Int32, Predicate<T>)

You could also use PriorityQueue and check within the supplied Comparator

PriorityQueue<String> p = new PriorityQueue<>((a, b) -> {
      if (a.length() > b.length()) {
        throw new RuntimeException(a);
      return 0;

    try {
    } catch (RuntimeException e) {

Arrays and References | Think Java, The lighter numbers outside the boxes are the indexes used to identify each location in the array. As with strings, the index of the first element is 0, not 1. For this� Examples. The example calls the following three overloads of the IndexOf method to find the index of a string in a string array:. IndexOf(Array, Object), to determine the first occurrence of the string "the" in a string array.

Indexes — MongoDB Manual, If an appropriate index exists for a query, MongoDB can use the index to limit to create a multikey index if the indexed field contains an array value; you do not� Python Numpy : Select an element or sub array by index from a Numpy Array 1 Comment Already Jim - April 21st, 2020 at 6:36 am none Comment author #29855 on Find the index of value in Numpy Array using numpy.where() by

Array.SetValue Method (System), value cannot be cast to the element type of the current Array. methods can determine whether any of the values in the indices array is out of bounds. For more� 3. After the extent is fixed, the indeterminate array can be used like a regular fixed array. 4. An indeterminate array can not be resized without resetting it. Trying to do so results in "Indeterminate extent is already fixed to a dimension of . (13738)"

IndexOutOfRangeException Class (System), An IndexOutOfRangeException exception is thrown when an invalid index is used to Attempting to assign an array element to another array that has not been If you forget to check whether the search operation found a match, the runtime� First off, I'll assume the gridData is an array, not an object as you've shown in your sample code because an object doesn't have a .push() method, but an array does. Use .length - 1 as the index to the last item you pushed onto the array or save the returned value from .push() which is the new length of the array. This will be the index of the element that you just pushed onto the array and will be valid until you modify the array before that index (adding or removing items before that index).

  • and why is 0 not the correct response?
  • @Stultuske Because that one which violate the rule is on position 2.
  • The problem is because you use indexOf which will occur at first match. In your case at index 0. Why don't simply use System.out.println(i+1); ?
  • And what should happen when Strings are equal?