简体   繁体   English

比较Java中的字符串数组中的元素

[英]Compare elements in an array of string in Java

I have to write a method that will compare elements in an array of strings and return the index of the largest element. 我必须编写一个方法,该方法将比较字符串数组中的元素并返回最大元素的索引。 It's going to be done recursively using a divide and conquer approach. 这将使用分而治之的方法递归完成。 I have an idea, and I was just looking to see if my idea was right or if it should be done in a different way. 我有一个主意,我只是想看看我的主意是否正确,或者是否应该以其他方式完成。

I was planning on looking at the array from the left side to mid -1, then look at mid , and then look at mid +1 to right. 我正打算从左侧看阵列mid -1,然后看mid ,再看看mid +1右。 I have a variable that will keep track of the largest index, and then after that make the recursive call for the left side and the right side. 我有一个变量,它将跟踪最大的索引,然后在那之后对左侧和右侧进行递归调用。

Does that sound like a good way to approach this problem? 听起来像是解决此问题的好方法吗?

This is what I have so far: 这是我到目前为止的内容:


public int longest()
{
    longest(0, a.length-1);
    return longestIndex;
}

private int longest( int left, int right)
{
    int longestIndex;
    int mid;

    if(left > right)
    {
        longestIndex = -1;
    }
    else if(left == right)
    {
        longestIndex = 0;
    }

    else
    {
        longestIndex = 0;

        mid = (left + right) / 2;
        longest(left, mid - 1);
        if (a[mid].compareTo(a[longestIndex]) > 0)
        {
            longestIndex = mid;
        }
        longest(mid + 1, right);            
    }            
    return longestIndex;
}

Also, since the methods are supposed to return an int, how would I pass the longestIndex n the private method up to the public method so that it would show up in my test program when longest is called? 另外,由于方法应该返回一个int,我如何将longestIndex n从private方法传递给public方法,以便在调用longest时将其显示在我的测试程序中?

Does it have to be recursive? 必须是递归的吗? Using recursion for this sounds like a case of: 为此使用递归听起来像是以下情况:

金锤

And your recursion looks totally wrong anyways, because not only you are not keeping track of the actual index but also your base cases and recursive calls don't make any sense. 无论如何,您的递归看起来都是完全错误的,因为不仅您没有跟踪实际的索引,而且您的基本情况和递归调用也没有任何意义。


If I were compelled to use recursion, I would do something like: 如果我被迫使用递归,我会做类似的事情:

int longest(array):
    return longest_helper(0, 0, array)

int longest_helper(max_index, curr_idx, array):
    # base case: reached the end of array
    if curr_idx == array.length:
        return max_index

    if array[curr_idx].length > array[max_index].length:
        max_index = curr_idx

    # recursive call
    return longest_helper(max_index, curr_idx + 1, array)

And then I would proceed to drop the class and tell the professor to give students problems where recursion is actually helpful next time around. 然后我将继续上课,并告诉教授给学生一些问题,在下一次递归实际上有帮助的地方。


Since it doesn't look like the array is sorted, the easiest (and fastest) way to do this would just be go through the whole thing (pseudocode): 由于看起来好像数组没有排序,所以最简单(最快)的方法就是遍历整个过程(伪代码):

max_index  = 0
max_length = array[0].length

for index in 1 .. array.length:
    if array[index].length > max_length:
        max_length = array[index].length
        max_index  = index

return max_index

This is your fourth question in two days on recursion. 这是您递归进行两天后的第四个问题。 It is good that you are putting homework tag but your time would be better spent understanding how recursion works. 您最好贴上作业标签,但最好花时间了解递归的工作原理。

My recommendation is to take a few colored discs (poker chips or playing cards of a single suite work well), work out manually the recursive solution to Towers of Hanoi and then come back and look at the individual questions you have been asking. 我的建议是拿一些彩色光盘(单套的扑克筹码或扑克牌效果很好),手动制定针对河内塔的递归解决方案,然后再回来查看您一直在问的单个问题。

In all likelihood you will be able to answer all the questions yourself. 您极有可能自己回答所有问题。 You would also be able to accept the answers, increasing you chances in future of getting responses when you face tougher questions. 您还将能够接受答案,从而在面对更棘手的问题时增加了将来获得答复的机会。

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

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