简体   繁体   English

创建我自己的myindexof方法

[英]Creating my own myindexof method

I'm a beginner in Java and received homework to create a replica of the indexOf method which receives a string as a parameter. 我是Java的初学者并且接受过功课来创建indexOf方法的副本,该方法接收字符串作为参数。 I have to check if the string received is a substring of the original string, and if so, I have to return the index of it. 我必须检查收到的字符串是否是原始字符串的子字符串,如果是,我必须返回它的索引。 For example: if the original string is "mother", str == "other" will return 1. If str is not a substring then return -1. 例如:如果原始字符串是“mother”,则str ==“other”将返回1.如果str不是子字符串,则返回-1。 I have to create it using only the methods length() and/or charAt() of String class. 我必须仅使用String类的length()和/或charAt()方法创建它。

I'm stuck on it for a long time. 我坚持了很长时间。 I've tried many sorts of codes but nothing succeeds... 我尝试了很多种代码,但没有成功......

For example: 例如:

public int myIndexOf1(String str)
{
    String objectStr = this._st;
    Word w3 = new Word(objectStr);
    Word w4 = new Word(str);
    char[] array = w3.toCharacterArray();

    int firstShowIndex = 0;
    int length = array.length;
    int max = objectStr.length() - str.length(); 
    for (int index = 0; index < max; index++)
    {
        for (int indexSubstring = 0; indexSubstring < str.length(); indexSubstring++)
        {
            if (objectStr.charAt(index) == str.charAt(indexSubstring))
            {
                firstShowIndex = index;
                break;
            }
            else
                firstShowIndex = -1;
        }
    }

    return firstShowIndex;
}

Please help! 请帮忙! Thanks in advance! 提前致谢!

Here's a solution that I came up with: 这是我提出的解决方案:

Note : It is not within the context of a class that contains a String as a private member as your does, but you can adapt it. 注意 :它不在包含String作为私有成员的类的上下文中,但您可以对其进行调整。

public static int myIndexOf (String mainStr, String otherStr)
{
    // either is null
    if (mainStr == null || otherStr == null)
    {
        return -1;
    }

    int len = mainStr.length();
    int lenOfOther = otherStr.length();

    // special case: both strings are empty
    if (len == 0 && lenOfOther == 0)
    {
        return 0;
    }

    // for each char in the main string
    for (int i = 0; i < len && len - i >= lenOfOther; i++)
    {
        // see if we can match char for char in the otherStr
        int k = 0;
        while (k < lenOfOther && mainStr.charAt(k + i) == otherStr.charAt(k))
        {
            k++;
        }
        if (k == lenOfOther)
        {
            return i;
        }
    }

    // nothing found
    return -1;
}

Usage : 用法

public static void main(String[] args)
{
    String mainStr = "mother";
    String otherStr = "other";

    int index = myIndexOf(mainStr, otherStr);
    System.out.println("My Index: " + index);

    // Just for a sanity check
    System.out.println("String Index: " + mainStr.indexOf(otherStr));
}

Outputs : 产出

My Index: 1
String Index: 1

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM