Java and javascript search array subscript comparison, main method, strong type language, weak type language, difference, index, Scanner, print, nextInt, println, main, break


Find the subscript (index) of the first occurrence of an element

java

Design a method to find the index position of the element in the array
Find the element, the index position of the first occurrence in the array
Know an array arr = {6, 36, 68, 86, 50};
Input a data with the keyboard, find the index of the data in the array.
And output the found index value on the console.
If not found, output-1

package PIndexOf;

import java.util.Scanner;

public class CIndexOf {
    
    
    public static void main(String[] args) {
    
    
        int[] arr = {
    
    6, 36, 68, 86, 50};

        Scanner sc = new Scanner(System.in);

        // 同行打印
        System.out.print("请输入您要查找的元素: ");
        int num = sc.nextInt(); // 68

        // 换行打印
        System.out.println(indexOf(arr, num)); // 2
    }

    public static int indexOf(int[] arr, int num) {
    
    
        int index = -1;

        for (int i = 0; i < arr.length; i++) {
    
    
            int item = arr[i];

            if (item == num) {
    
    
                index = i;
                break;
            }
        }

        return index;
    }
}

javascript

function indexOfF(arr, num) {
    
    
	var index = -1;
	
	for (let i = 0; i < arr.length; i++) {
    
    
		const item = arr[i];
		
		if (item === num) {
    
    
			index = i;
			break;
		}
	}
	
	return index;
}

let arr = [6, 36, 68, 86, 50];
console.log(indexOfF(arr, 86)); // 3

the difference

1. For the console, javathere is a keyboard input function; if javascriptyou write to death directly, you need to write through the page, that is htmlthe knowledge.
2. The printing method javacan be directly printed with newline API; javascriptit needs to be realized by character splicing.
3. There are different ways to define variables, which are not described in detail here.


common ground

1. The logic process of implementation is the same.


Find the subscript (index) of multiple occurrences of an element

java

Requirement: Design a method to find the index position of the element in the array (considering the problem of repeated elements)
Note: returnOnly one result can be returned
Question: When a method finishes running, there are multiple results that need to be returned, how to deal with it
Answer: You can store multiple results in an array container and return the array

package PIndexOf;

import java.util.Scanner;

public class CIndexOf {
    
    
    public static void main(String[] args) {
    
    
    	int[] arr = {
    
    19, 28, 37, 46, 50, 19, 19};

        int[] results = indexOf(arr, 19);

        if (results.length == 0) {
    
    
            System.out.println("您要查找的元素不存在于数组中");
        } else {
    
    
            for (int i = 0; i < results.length; i++) {
    
    
                // 0
                // 5
                // 6
                System.out.println(results[i]);
            }
        }
    }

    public static int[] indexOf(int[] arr, int num) {
    
    
        int count = 0;

        for (int i = 0; i < arr.length; i++) if (arr[i] == num) count++;

        int[] result = new int[count];

        int index = 0;

        for (int i = 0; i < arr.length; i++) {
    
    
            if (arr[i] == num) {
    
    
                result[index] = i;
                index++;
            }
        }

        return result;
    }
}

javascript

function indexOfF(arr, num) {
    
    
	var result = [];
	
	for (let i = 0; i < arr.length; i++) {
    
    
		const item = arr[i];
		
		if (item === num) result.push(i);
	}
	
	return result;
}

let arr = [68, 36, 68, 86, 50, 7];
console.log(indexOfF(arr, 68)); // (2) [0, 2]

the difference

1. In the printing method, javathe array cannot be printed directly, and the address value of the array is printed on the console.
2. Logical process, javathe defined array needs to be set to a fixed length or a fixed value, and one more cycle is required to obtain the number of matches, so as to define a fixed-length array by the number; javascriptthere are dynamic arrays API.
3. The method of defining an array is different from the syntax. javaTo define an array, you need to specify the value type. The syntax is . {}To javascriptdefine an array, you don’t need to specify the type, and you don’t need to know the final length of the array. The syntax is [].
4. The return result is different, and javathe return result needs to be determined when the method is defined; javascriptit does not care about the return result and has nothing to do with the method definition.


Summarize

A conclusion can be drawn from the above example: javait is a strongly typed language; javascriptit is a weakly typed language.

Guess you like

Origin blog.csdn.net/weixin_51157081/article/details/131866686