小编Jac*_* L.的帖子

查找数组中整数的众数

对于这个问题,我将编写一个名为 mode 的方法,该方法返回整数数组中出现频率最高的元素。假设数组至少有一个元素,并且数组中的每个元素都有一个介于 0 和 100 之间的值。通过选择较低的值打破联系。

例如,如果传递的数组包含值 {27, 15, 15, 11, 27},则您的方法应返回 15。(提示:您可能希望查看本章前面的 Tally 程序以了解如何解决这个问题呢。)

我在查看特定输入出了什么问题时遇到了问题。例如:

mode({27, 15, 15, 27, 11, 11, 11, 14, 15, 15, 16, 19, 99, 100, 0, 27}) 返回 15,这是正确的,但是 mode({1, 1, 2, 3, 3}) 当它应该是 1 时返回 3。

这是代码:

public static int mode(int[] input) {
    int returnVal = input[0]; // stores element to be returned
    int repeatCount = 0; // counts the record number of repeats
    int prevRepCnt = 0; // temporary count for …
Run Code Online (Sandbox Code Playgroud)

java arrays mode

5
推荐指数
2
解决办法
3万
查看次数

以最小的运行时间存储具有递归的Fibonacci序列的值

我知道我的代码现在有很多问题,但我只是想在尝试任何事情之前弄清楚这些想法.我需要一个接受整数n的方法,该整数n返回Fibonacci序列中的第n个数字.虽然通过递归正常解决它,但我必须最小化运行时,所以当它得到类似于第45个整数的东西时,它仍然会相当快地运行.另外,我不能使用类常量和全局变量.

正常的方式w /递归.

public static int fibonacci(int n) {
    if (n <= 2) { // to indicate the first two elems in the sequence 
        return 1;
    } else { // goes back to very first integer to calculate (n-1) and (n+1) for (n)
        return fibonacci(n-1) + fibonacci(n-2); 
    }
}
Run Code Online (Sandbox Code Playgroud)

我认为问题是这个过程中有很多冗余.我想我可以创建一个List来计算最多第n个元素,所以它只在我返回第n个元素之前运行一次.但是,在这种情况下,我很难看到如何使用递归.

如果我正确理解它,标准的递归方法很慢,因为有很多重复:

fib(6)= fib(5)+ fib(4)

fib(5)= fib(4)+ fib(3)

fib(4)= fib(3)+ 1

fib(3)= 1 + 1

这是接近这个的正确方法吗?是否需要使用某种形式的容器来获得更快的输出,同时仍然是递归的?我应该使用辅助方法吗?我刚刚进入递归编程,因为我已经习惯了迭代方法,所以很难解决这个问题.谢谢.

这是我有缺陷和未完成的代码:

public static int fasterFib(int n) {
    ArrayList<Integer> results = new ArrayList<Integer>();
    if (n <= …
Run Code Online (Sandbox Code Playgroud)

java recursion containers fibonacci

2
推荐指数
1
解决办法
5499
查看次数

通过对象添加时间

对于这个问题,我必须在一个名为TimeSpan的对象中添加一个mutator实例方法.当添加的分钟数高于59时,我无法正确获得小时和分钟; 至少我有几个小时.

这就是我所拥有的:

public void add(TimeSpan span) {
    this.hours += span.hours;

    if ((this.minutes + span.minutes) >= 60) { 
        this.hours += (this.minutes + span.minutes)/60;
        this.minutes += (this.minutes + span.minutes)%60;
    } else {
        this.minutes += span.minutes;
    }
}
Run Code Online (Sandbox Code Playgroud)

java time object

0
推荐指数
1
解决办法
1080
查看次数

如何从一组中删除

此方法接受一组字符串,然后删除该组的偶数长度的所有字符串.问题是我知道集合不计入元素所以我必须使用迭代器,但是,如何从集合中删除特定的"元素"?

private static void removeEvenLength(Set<String> thing) {
    Iterator<String> stuff = thing.iterator();

    while (stuff.hasNext()) {
        String temp = stuff.next();
        if (temp.length() %2 == 0) {
            temp.remove(); // What do I do here?
        }
    }
}
Run Code Online (Sandbox Code Playgroud)

java iterator set

0
推荐指数
1
解决办法
220
查看次数

为什么indexOf()没有识别空格?

对于此程序,它要求用户输入其全名.然后通过在名字和名字之间放置的空格中分隔它们来排序名字和姓氏.但是,indexOf()不识别空格,只返回-1.这是为什么?谢谢.

以下是PracticeIt的提示:

编写一个名为processName的方法,该方法接受控制台的扫描程序作为参数,并提示用户输入其全名,然后以相反的顺序打印名称(即姓氏,名字).您可以假设只会给出名字和姓氏.您应该使用扫描仪一次读取整行输入,然后根据需要将其拆分.以下是与用户的示例对话:

请输入您的全名:Sammy Jankis

你的名字是相反的顺序是Jankis,Sammy

import java.util.*;

public class Exercise15 {

public static void main(String[] args) {
    Scanner inputScanner = new Scanner(System.in);
    processName(inputScanner);

}

public static void processName(Scanner inputScanner) {
    System.out.print("Please enter your full name: ");
    String fullName = inputScanner.next();

    int space = fullName.indexOf(" "); // always return -1 for spaces
    int length = fullName.length();

    String lastName = fullName.substring(space+1,length+1);
    String firstname = fullName.substring(0, space);

    System.out.print("Your name in reverse order is " + lastName + ", " + firstname);
} …
Run Code Online (Sandbox Code Playgroud)

java string char indexof

0
推荐指数
1
解决办法
1022
查看次数

为什么我不能对此ArrayList进行排序?

我正在从我的教科书中复制一个例子,但它拒绝编译.我在某个地方打错了吗?出于某种原因,在客户端代码上,Collections.sort(words)不允许程序编译.任何帮助表示赞赏.代码复制自Stuart Reges和Marty Stepp的"构建Java程序"第2版.我试图通过复制来理解它.

该程序应该创建一个CalendarDate对象以放入ArrayList.通过实现CalendarDate的Comparable接口,我可以使用Collections.sort按顺序在该arraylist中对生日进行排序.但是,这不起作用b/c Collections.sort(日期)将无法运行.

客户端代码(包含问题):

import java.util.*;

// Short program that creates a list of birthdays of the
// first 5 U.S. Presidents and that puts them into sorted order.
// We can now use Collections.sort for ArrayList<CalendarDate> b/c CalendarDate implements the Comparable interface. 

public class CalendarDateTest {
    public static void main(String[] args) {
        ArrayList<CalendarDate> dates = new ArrayList<CalendarDate>(); // Creates a new ArrayList of 'CalendarDate' object type.

        // adds a new CalendarDate object with month = 2 and …
Run Code Online (Sandbox Code Playgroud)

java sorting interface arraylist comparable

0
推荐指数
2
解决办法
1229
查看次数