Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java int array toString方法,该方法显示的值之间有空格,每十个值显示一行_Java_Arrays_Recursion_Methods - Fatal编程技术网

Java int array toString方法,该方法显示的值之间有空格,每十个值显示一行

Java int array toString方法,该方法显示的值之间有空格,每十个值显示一行,java,arrays,recursion,methods,Java,Arrays,Recursion,Methods,谈到java,我是一个初学者,我遇到了一些问题(整天坐在这里)。我到处寻找解决办法,但都没有结果。我的教授让我填写空白的方法,尤其是ToStand方法(尽管我对其他方面也感觉不好),任何帮助都会被赏识。p> import java.util.Random; public class SmartArray { // declare an array of ints int[] list; Random r = new Random(); int pos = 0;

谈到java,我是一个初学者,我遇到了一些问题(整天坐在这里)。我到处寻找解决办法,但都没有结果。我的教授让我填写空白的方法,尤其是ToStand方法(尽管我对其他方面也感觉不好),任何帮助都会被赏识。p>
import java.util.Random;

public class SmartArray {
    // declare an array of ints

    int[] list;
    Random r = new Random();
    int pos = 0;

    /**
     * Creates and initializes an array of size n. It does not initialize the
     * contents of the array.
     */
    public SmartArray(int n) {
        list = new int[n];

    }

    /**
     *
     * Initializes the contents of the array with random
     *
     * non-negative* numbers.
     *
     */
    public void initRandom() {
        int i = 0;
        int hold = 0;
        while (i < list.length) {
            hold = r.nextInt();
            if (hold % 2 == 0) {
                list[i] = hold;
                i++;
            } else {
                hold = hold + 1;
                list[i] = hold;
                i++;
            }

        }
    }

    /**
     *
     * Allows client code to add an element to the array
     *
     * at the given position. Throws an ArrayIndexOutOfBounds
     *
     * exception with a message if the
     *
     * position is out of bounds.
     *
     */
    public void insert(int value, int pos) {
        if (list.length > pos) {
            list[pos] = value;
        } else {
            throw new ArrayIndexOutOfBoundsException("The position of your value is greater than the array");

        }
    }

    /**
     *
     * Returns the position of target if target is
     *
     * one of the values in the array, otherwise -1.
     *
     * Implemented with a loop.
     *
     */
    public int find(int target) {
        int position = 0;
        for (int i = 0; i < list.length; i++) {
            if (list[i] == target) {
                position = i;
            } else {
                position = -1;
            }
        }
        return position;

    }

    /**
     *
     * Same as the find method, except that it's implemented
     *
     * using recursion. (Hint: use a helper method.)
     *
     */
    public int recursiveFind(int pos, int target) {
        if (pos >= list.length) {
            return -1;
        } else if (list[pos] == target) {
            return pos;
        } else {
            return recursiveFind(pos + 1, target);
        }

    }

    /**
     *
     * Returns the elements of the array, separated by
     *
     * spaces, with a newline after every 10 elements,
     *
     * so they can be easily displayed.
     *
     */
    public String toString() {
        String listString = "";
        int pos = 0;
        for (int i = 0; i < list.length; i++) {
            //list[i].toString();
            listString = listString + (String) list[i] + " ";
            pos++;
            if (pos > 9) {
                list = list + "\n";
                pos = 0;
            }
            return listString;
        }

    }
}
import java.util.Random;
公共类智能阵列{
//声明一个整数数组
int[]列表;
随机r=新随机();
int pos=0;
/**
*创建并初始化大小为n的数组。它不会初始化
*数组的内容。
*/
公共智能阵列(int n){
列表=新整数[n];
}
/**
*
*使用随机变量初始化数组的内容
*
*非负*数字。
*
*/
public void initRandom(){
int i=0;
int hold=0;
while(ipos){
列表[pos]=值;
}否则{
抛出新的ArrayIndexOutOfBoundsException(“值的位置大于数组”);
}
}
/**
*
*如果目标为,则返回目标的位置
*
*数组中的一个值,否则为-1。
*
*通过循环实现。
*
*/
公共整数查找(整数目标){
int位置=0;
for(int i=0;i=列表长度){
返回-1;
}否则如果(列出[pos]==目标){
返回pos;
}否则{
返回递归查找(位置+1,目标);
}
}
/**
*
*返回数组的元素,以
*
*空格,每10个元素后有一个换行符,
*
*因此,它们可以很容易地显示出来。
*
*/
公共字符串toString(){
字符串listString=“”;
int pos=0;
for(int i=0;i9){
列表=列表+“\n”;
pos=0;
}
返回listString;
}
}
}
那么:

public String toString() {
    String listString = "";
    for (int i = 1; i < list.length; i++) {
        listString += list[i - 1] + (i % 10 == 0 ? "\n" : " ");
    }
    return listString;
}
公共字符串toString(){
字符串listString=“”;
for(int i=1;i
您的方法看起来很好

要添加换行符,应该检查索引的其余部分

您还将在循环中返回生成的
字符串
。你应该在它之后才回来

public String toString() {

    String listString = "";
    for (int i = 0; i<list.length; i++){
        listString = listString + (String)list[i] + " ";
        if (i%9 == 0){ // if remainder of i%9 = 0, then add a newline
            list = list + "\n";
        }

    }
    return listString;
}
公共字符串toString(){
字符串listString=“”;

对于(int i=0;i,我不想给出太多的偏离,因为它是一个学术任务,而不是<代码> POS < /代码>,考虑MOD(<代码> %>代码>),你使用它,所以我知道你熟悉它。

考虑从1开始
i
,并遍历所有值(无需每次取10)。但测试是否需要这样的换行符:

i % 10 == 0
每当第i个值可被10整除时,插入换行符

也可以考虑使用<代码>整型< /代码>代替<代码> int <代码>。这样,您可以简单地做到这一点:

listString = listString + list[i].toString() + " ";
看看这个,就这样


祝你好运!

这是OP的问题,这段代码是否运行?主要部分在哪里?公共静态void main(String[]args)能否发布运行当前代码得到的输出以及您期望/需要的正确输出?