Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

//package com.java2s;

import java.util.ArrayList;

public class Main {
    /**
     * returns the index of the serached string from the array. If not found returns -1.
     * 
     * @param strValues
     * @param strSearchedValue
     * @return
     */
    public static int getIndex(String[] strValues, String strSearchedValue) {

        // checks for null input
        if (strValues == null || strSearchedValue == null)
            return -1;

        // cycles on the vcalues of the array
        for (int j = 0; j < strValues.length; j++) {

            // gets the j-th element
            String strVal = strValues[j];

            // if is the searched one, returns its index
            if (strVal.equals(strSearchedValue))
                return j;
        }

        // if arrives here, the searched value hasn't been found
        return -1;
    }

    /**
     * returns the index of the serached string from the array. If not found returns -1.
     * 
     * @param strValues
     * @param strSearchedValue
     * @return
     */
    public static int getIndex(ArrayList<String> alValues, String strSearchedValue) {

        // cycles on the vcalues of the array
        for (int j = 0; j < alValues.size(); j++) {

            // gets the j-th element
            String strVal = alValues.get(j);

            // if is the searched one, returns its index
            if (strVal.equals(strSearchedValue))
                return j;
        }

        // if arrives here, the searched value hasn't been found
        return -1;
    }
}