Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/14.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 将数组打印为每行10个元素的字符串_Java_Arrays - Fatal编程技术网

Java 将数组打印为每行10个元素的字符串

Java 将数组打印为每行10个元素的字符串,java,arrays,Java,Arrays,上面的代码是我的主代码的一部分,在主代码中,我获取用户输入并作为它们设置数组的边界条件。案例“b”要求通过调用类中的函数打印出数组,该函数应以每行10个元素的字符串形式返回数组 // Line in Main Code public class Assignment7 { public static void main(String[] args) { Scanner scan = new Scanner(System.in); String input; char u

上面的代码是我的主代码的一部分,在主代码中,我获取用户输入并作为它们设置数组的边界条件。案例“b”要求通过调用类中的函数打印出数组,该函数应以每行10个元素的字符串形式返回数组

// Line in Main Code 
public class Assignment7 {
public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    String input;
    char userChoice;
    int newVal, index;
    IntegerList intList = null;

    printMenu();

    do {
        System.out.print("Please enter a command or type? ");
        input = scan.nextLine();
        if (input.length() != 0)
            userChoice = input.charAt(0);
        else
            userChoice = ' ';
        switch (userChoice) {
        case 'a':
            System.out.print("How big should the list be? ");
            intList = new IntegerList(scan.nextInt());
            scan.nextLine();
            System.out.print("What is range of the values for each random draw? ");
            intList.randomize(scan.nextInt());
            scan.nextLine();
            break;
        case 'b':
            System.out.println(intList.toStrng());
            break;
//类中的行
导入java.util.array;
导入java.util.Random;
公共类整数列表{
私人规模;
公共整数列表(整数大小){
大小=大小;
}   
私有int[]IntArray=新int[arrSize];
公共无效随机化(int num){

对于(inti=0;i请看如何通过当前构造函数创建整数数组

// line in class
import java.util.Arrays;
import java.util.Random;
public class IntegerList {

private int arrSize;

public  IntegerList(int size) {
    size = arrSize;
}   

private int[] IntArray = new int[arrSize];

public void randomize (int num) {
    for(int i = 0;i<IntArray.length;i++) {
        IntArray[i] =(int) (Math.random()*(num+1));
    }

}   


public void addElement(int newVal, int index) {
    for(int i = index;i<IntArray.length;i++) {
        int temp = IntArray[i];
        IntArray[i]=newVal;
        IntArray[i+1]=temp;
        if(i == IntArray.length){
            increaseSize(IntArray);
        }
    }

}

private static void increaseSize(int[] x) {
    int[] temp = new int[2*x.length];
    for(int i = 0; i<x.length;i++) {
        temp[i]=x[i];
    }
    x = temp;
}

public void removeFirst(int nextInt) {
    // TODO Auto-generated method stub

}

public String range() {
    // TODO Auto-generated method stub
    return null;
}

public String toStrng() {
    String arrayOut = " ";
    for(int i = 0; i<IntArray.length; i++ ) {
    if(i%10 == 0 ) {
        arrayOut+="\n";
    }

    arrayOut += IntArray[i] + " " ;
    }

    return arrayOut;
}

} 
当你打电话的时候

public  IntegerList(int size) {
    size = arrSize;
}   

private int[] IntArray = new int[arrSize];
在您的菜单程序中,您的数组列表不会神奇地知道它需要重新初始化。它将为0,因为创建IntegerList对象时arrSize的值始终为0。此外,您还切换了变量的赋值。将您的构造函数更改为

intList = new IntegerList(scan.nextInt());

一切似乎都正常,因为数组的大小始终为0,而您的方法只是返回。

您没有初始化arrSize
您可以修改构造函数以初始化arrSize
公共整数列表(整数大小){ arrSize=大小; }


另外,在您的toStrng方法中,您可以使用arrSize而不是IntArray.length

在您的代码中,您认为什么应该产生您想要的显示?提示:您需要打印语句。仍然没有改变任何内容。我忘记在问题中添加println行,但它是e代码,仍然没有输出
private int[] IntArray = null;

public  IntegerList(int size) {
    arrSize = size;
    IntArray = new int[arrSize];
}