Java Create a custom array _java

Source: Internet
Author: User

1.Java to create a custom class array method:

Student []stu = new student[3];
for (int i = 0; i < 3; i + +)
{
Stu[i] = new Student ();
}

2. Otherwise you will be prompted null pointer exception

Package project;
Import java.io.*;
Import Java.util.Scanner;
  Class Student {private int id;
  private String name;
   
  private int score;
  public void setId (int id) {this.id = ID;
  public int getId () {return this.id;
  public void SetName (String name) {this.name = name;
  Public String GetName () {return this.name;
  The public void SetScore (int score) {This.score = score;
  public int Getscore () {return this.score;
  } public class Project2 {File File = new file ("E:/data.txt");
  FileWriter filewrite = null;
  BufferedWriter write = null;
  FileReader fileread = null;
  BufferedReader read = null;
  Student []stu = new student[3];
    public void put () {try {filewrite = new FileWriter (file);
    catch (IOException e) {//TODO automatically generated catch block E.printstacktrace ();
    Write = new BufferedWriter (filewrite); for (int i = 0; i < 3; i + +) {System.out.println ("Please enter the first" + (i +)1 + "A student's ID, name, Grade:");
      Scanner in = new Scanner (system.in);
        try {String str = in.nextline ();
        String data[] = Str.split ("");
          for (int j = 0; J < 3; J + +) {Write.write (data[j]);
        Write.newline ();
      The Catch Block E.printstacktrace () is automatically generated by catch (IOException e) {//TODO);
      } try {write.close ();
    Filewrite.close ();
    catch (IOException e) {//TODO automatically generated catch block E.printstacktrace ();
    The public void get () {int sum = 0;
    Double Ave;
    try {fileread = new FileReader (file);
    catch (FileNotFoundException e) {//TODO automatically generated catch block E.printstacktrace ();
    Read = new BufferedReader (fileread);
      for (int i = 0; i < 3; i + +) {Stu[i] = new Student ();
        try {stu[i].setid (Integer.parseint (Read.readline ()));
        Stu[i].setname (Read.readline ()); Stu[i].setScore (Integer.parseint (Read.readline ()));
      catch (Exception e) {//TODO automatically generated catch block E.printstacktrace ();
    for (int i = 0; i < 3; i + +) {sum + = Stu[i].getscore ();
    Ave = SUM * 1.0/3;
    SYSTEM.OUT.PRINTLN ("Student's average score is:" + ave);
      try {read.close ();
    Fileread.close ();
    catch (IOException e) {//TODO automatically generated catch block E.printstacktrace ();
    } public static void Main (String []args) {Project2 Pro = new Project2 ();
    Pro.put ();
  Pro.get (); }
}

Summarize:

This allows us to define the desired array within the project, depending on the requirements of the project.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.