首页 文章

开始Java方法实践

提问于
浏览
-2

方法:fillArray

该方法接受非null String变量作为参数 . 返回一个已从传递给方法的String填充的char数组,每个元素设置为字符串中的相应字符 . public char [] fillArray(String var)

方法:stringArray

如果chArray为null或length为零,则该方法接受chars数组作为参数,如果chArray不为null,则返回空字符串,返回字符串中数组中每个字符的单个字符串 . public String stringArray(Char [] chArray)

方法:productMatrix 0 <= n <= 100如果n超出边界,如果n大于或等于零则返回null,返回一个n×n矩阵,矩阵的每个元素设置为行索引*列索引 . public int [] [] productMatrix(int n)

方法:sumOfSquares如果数组为null或大小为零,则该方法接受一个int数组,如果数组的大小大于或等于1,则返回0返回一个int,该int等于每个元素的平方和array public long sumOfSquares(int [] intArray)

方法:incPosArray如果数组为null或大小为零,则方法接受一个int数组,如果数组的大小大于或等于1,则返回数组的每个正元素,只留下负数元素和元素等于零设置为它们的传入值public void incPosArray(int [] intArray)

public class Q3 {


public char[] fillArray(String var){
    System.out.println( "fillArray not implemented");
    return null;
}


public String stringArray(char[] chArray) {
    System.out.println( "stringArray not implemented");
    return null;
}


public int[][] productMatrix(int n){
    System.out.println( "productMatrix not implemented");
    return null;
}


public long sumOfSquares (int[] intArray) {
    System.out.println( "sumOfSquares not implemented");
    return -1;
}


public void incPosArray (int[] intArray) {
    System.out.println( "incPosArray not implemented");
    return;
}


public static void main (String[] args) {
    Q3 q3 = new Q3();

    //Put your test code here

}
}

1 回答

  • 1

    这显然是家庭作业,所以我不是为你做这一切,但我会帮助你得到一个,所以也许你有一些想法如何处理其余...

    这是你填充数组方法的事情......

    public char[] fillArray(String var){
        //is the string NOT null? 
        if(var != null){
    
            // create new a new char array with a length equal to the passed in String  
            char[] splitString = new char[var.length()];
    
            /*String function - for breaking down our string in chars. 
            * args 1 = the letter we want to start from, in this case the begging i.e. 0
            * args 2 = the letter we want to end at...well the end of the string so just pass its length
            * args 3 = the char array we made and want to put the chars into!
            * args 4 = no idea, just leave it as zero, something to do with offset
            */
            var.getChars(0, var.length(), splitString, 0);
            //then just return the string
            return splitString;
        }
    
        //I am assuming that if the string is null we return null????
        //this only calls if the string is null
        return null;
    }
    

    用波纹管代码测试它......

    char [] array = fillArray("hello");
    
        for(int i = 0; i < array.length; i++){
            System.out.println(array[i]);
        }
    

相关问题