小编Exc*_*ion的帖子

Java,编写自己的split string方法

我需要能够编写自己的分割字符串方法,以便输入像

String[] test1 = mySplit("ab#cd#efg#", "#");
System.out.println(Arrays.toString(test1));
Run Code Online (Sandbox Code Playgroud)

将打印[ab, #, cd, #, efg, #]到控制台.到目前为止,我已经让它像那样拆分但是我的方式留下了两个分隔符连续的尴尬空间,或者分隔符位于输入的开头.

public static String[] mySplit(String str, String regex)
{
    String[] storeSplit = new String[str.length()];
    char compare1, compare2;
    int counter = 0;

    //Initializes all the string[] values to "" so when the string
    //and char concatonates, 'null' doesn't appear.
    for(int i=0; i<str.length(); i++) {
        storeSplit[i] = "";
    }

    //Puts the str values into the split array and concatonates until
    //a delimiter is found, then it moves to …
Run Code Online (Sandbox Code Playgroud)

java string split

9
推荐指数
1
解决办法
5745
查看次数

理解递归的麻烦

好的,所以我有一个程序:

public class Rec {
    public static void main(String[] args) {
        test(5);
    }
    static void test(int n) {
        if (n > 0) {
        System.out.println(n);
        test(n-1);
        System.out.println(n);
        }
    }
Run Code Online (Sandbox Code Playgroud)

它的输出是5,4,3,2,1,1,2,3,4,5.我的问题是,为什么/如何执行第二个println(n)语句?我认为函数调用会完全切断它,而是以令我困惑的方式行事.这不是家庭作业或任何事情,我真的很难理解递归是如何工作的.

java recursion

3
推荐指数
1
解决办法
130
查看次数

释放动态内存

我最近做了一个学校的家庭作业和失分,在评论中,评分者说我没有正确地释放指针.下面是我发送的代码,我想知道如何正确释放指针?

/*Student: Daniel
 *Purpose: To reverse a string input using
 *pointers.
 */
#include <iostream>
#include <cstring>
#include <string>
using namespace std;

int main() {
    string input;
    char *head = new char, *tail = new char;
    char temp;
    //Get the string from the user that will be reversed
    cout << "Enter in a string that you want reversed: ";
    getline(cin, input);
    //Create and copy the string into a character array
    char arr[input.length()];
    strcpy(arr, input.c_str());
    //Set the points of head/tail to the …
Run Code Online (Sandbox Code Playgroud)

c++ memory pointers

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

标签 统计

java ×2

c++ ×1

memory ×1

pointers ×1

recursion ×1

split ×1

string ×1