如何通过我的静态方法将值传递给我的对象数组 [英] How to pass values to my object array from my static method

查看:41
本文介绍了如何通过我的静态方法将值传递给我的对象数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有3个类,一个EyMain是我的主类,在其中我读取一个 n值> 100 ,该对象创建我的对象数组的数量。然后调用 writeUSB()方法在其中填充对象数组。

I have 3 classes one EyMain that is my main class where I read a "n" value > 100 that creates the number of my object arrays.Then I call writeUSB() method where I fill the object array.

public class EyMain {

    public static void main(String[] args) {

        int n;

        do {       
            System.out.println("Give an integer value > 100 : ");
            n = scannerUserInput.getInteger();
        } while (n < 101);

        ekpaideytikoYliko usb[] = new ekpaideytikoYliko[n];

        eYMethods.writeUSB(usb);
        eYMethods.showDocs(usb);

        } 
}

我的另一类是eYMethods我有2个静态方法 writeUSB ,我想返回存储在数组中的最后一个元素的指针,因为我想检查我的 memorySpace> 8gb是否要从中删除数组并更新数组的最后一个元素 showDocs ,我只希望打印用户键入的对象数组中的元素,而只打印带有的文件扩展名。 doc或.docx

My other class is eYMethods where I have my 2 static methods writeUSB that I want to return the pointer of the last element that have stored in my array because I want to check if my memorySpace > 8gb I want to remove it from the array and updating the last element of the array and showDocs that I want to print only the elements from the object array that the user has typed and print only the file extension with .doc or .docx .

package eymain;

public class eYMethods {

static double writeUSB(ekpaideytikoYliko usb[]) {

    for(int i = 0; i < usb.length; i++) {      

        System.out.println("Give fileName : ");
        usb[i].setFileName(scannerUserInput.getString());
        System.out.println("Give minutes : ");
        usb[i].setMinutes(scannerUserInput.getDouble());
        System.out.println("Give memorySpace");
        usb[i].setMemorySpace(scannerUserInput.getDouble());
    }


    return 0;

}
static void showDocs(ekpaideytikoYliko usb[]) {

    for(int i =0; i < usb.length; i++) {
        System.out.println("fileName : " + usb[i].getFileName());
        System.out.println("minutes : " + usb[i].getMinutes());
        System.out.println("memorySpace : " + usb[i].getMemorySpace());
    }
}}

最后一堂课是我的ekapideytikoYliko私有变量,get和set,我的构造函数和一个String方法getFileType,我想从fileName中获取扩展名。例子(.doc,.docx,.mp4)。

And last class is my ekapideytikoYliko that I have my private variables, get and set, my constructor and a String method getFileType that I want to take from the fileName the extension from it. Example(.doc, .docx, .mp4).

package eymain;

public class ekpaideytikoYliko {

private String fileName;
private double minutes;
private double memorySpace;

ekpaideytikoYliko(String fileName, double minutes, double memorySpace) {

    this.fileName = fileName;
    this.minutes = minutes;
    this.memorySpace = memorySpace;

}

public String getFileName() {
    return fileName;
}

public void setFileName(String fileName) {
    this.fileName = fileName;
}

public double getMinutes() {
    return minutes;
}

public void setMinutes(double minutes) {
    this.minutes = minutes;
}

public double getMemorySpace() {
    return memorySpace;
}

public void setMemorySpace(double memorySpace) {
    this.memorySpace = memorySpace;
}

String getfileType(ekpaideytikoYliko usb[]) {

    int name = fileName.lastIndexOf(".");
    if (name == -1) {
        return "";
    }
    return fileName.substring(name);
}}

还有我的scannerUserInput文件:

And my scannerUserInput file :

package eymain;

import java.util.Scanner;

public class scannerUserInput {

    static int getInteger(){
    Scanner ob = new Scanner(System.in);
      try{
    int i = ob.nextInt();
        return i;
      }
      catch(Exception e){
    return -1;
      }
}

    static byte getByte(){
    Scanner ob = new Scanner(System.in);
      try{
    byte b = ob.nextByte();
        return b;
      }
      catch(Exception e){
    return -1;
      }
}
    static short getShort(){
    Scanner ob = new Scanner(System.in);
      try{
    short s = ob.nextShort();
        return s;
      }
      catch(Exception e){
    return -1;
      }
}
    static long getLongInteger(){
    Scanner ob = new Scanner(System.in);
      try{
    long l = ob.nextLong();
        return l;
      }
      catch(Exception e){
    return -1;
      }
}
    static float getFloat(){
    Scanner ob = new Scanner(System.in);
      try{
    float f = ob.nextFloat();
        return f;
      }
      catch(Exception e){
    return -1;
      }
}
    static double getDouble(){
    Scanner ob = new Scanner(System.in);
      try{
    double d = ob.nextDouble();
        return d;
      }
      catch(Exception e){
    return -1;
      }
}
    static String getString(){
    Scanner ob = new Scanner(System.in);
      try{
    String s = ob.nextLine();
        return s;
      }
      catch(Exception e){
    return "";
      }
}

    static char getChar(){
    Scanner ob = new Scanner(System.in);
      try{
    char ch = ob.next().charAt(0);
        return ch;
      }
      catch(Exception e){
    return ' ';
      }
}    

} 

当我在 writeUSB 方法中键入来自扫描仪的数据,我的第一种类型出现错误。

When I type inside the writeUSB method the data from the scanner I get an error in my first type.

推荐答案

在Java中,如果根据情况创建对象数组,则为:

In java when you create an array of an object in your case it is:

ekpaideytikoYliko usb[] = new ekpaideytikoYliko[n];

Java只是这样做

// Lets say n = 5 for easier demonstration
{null, null, null, null, null}

来源:变量的初始值


对于所有引用类型(第4.3节),默认值为

For all reference types (§4.3), the default value is null.

现在在 eYMethods 中尝试调用方法时只返回nullpointer,因为该元素为null。要解决此问题,您需要创建一个对象并将该对象存储在数组中。像这样的东西:

Now when in eYMethods when you try do call a method it just return nullpointer because that element is null. To fix that you need to create an object and store that object in array. Something like this:

for (int i = 0; i < usb.length; i++) {
    System.out.println("Give fileName : ");
    String fileName = scannerUserInput.getString();
    System.out.println("Give minutes : ");
    double minutes = scannerUserInput.getDouble();
    System.out.println("Give memorySpace");
    double memorySpace = scannerUserInput.getDouble();
    ekpaideytikoYliko tempEkpaideytikoYliko = new ekpaideytikoYliko(fileName, minutes, memorySpace);
    usb[i] = tempEkpaideytikoYliko;
}

现在,java将创建一个 ekpaideytikoYliko 并将其存储在数组中。

Now java will create an Object of Class ekpaideytikoYliko and store it in array.

这篇关于如何通过我的静态方法将值传递给我的对象数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆