📜  如何在Java返回一个数组?

📅  最后修改于: 2022-05-13 01:55:35.935000             🧑  作者: Mango

如何在Java返回一个数组?

数组是一种数据结构,它由一组相同数据类型的元素组成,这样数组的每个元素都可以由单个数组索引或键标识。数组元素的存储方式是,任何元素的地址都可以使用数组的第一个索引的位置使用简单的数学关系来计算。 Java中的数组与 C/C++ 中的数组相比,在实现和使用上有所不同,尽管它们也有许多相似之处。这里我们将讨论如何在Java返回一个数组。

为了在Java返回一个数组,我们需要注意以下几点:

Keypoint 1:返回数组的方法的返回类型必须是与被返回数组的数据类型相同的数组。返回类型也可以是通常的 Integer、Double、 字符、String 或用户定义的类对象。

// Method returning an String array.
int[] methodName() {...}
// Method returning a String array.
String[] methodName() {...}
// Method returning an array of objects of class named Students.
Students[] methodName() {...} 

关键点2:必须准确使用访问修饰符,考虑方法和返回数组的用法。还必须考虑静态和非静态声明。

// Using public access modifier and static to call the method from a static class, method or block.
public static char[] methodName() {...} 

关键点3:在方法调用处必须有任何相同数据类型的变量数组或类似的东西来处理返回的数组。例如,从任何方法返回的整数数组都可以按如下方式存储。



int[] storage = methodReturningArray();

执行:

为了更好地理解这一点,我们可以研究几种可能返回数组的不同场景。在这里,我们将考虑三个场景示例。

  • 案例 1:简单的内置数组
  • 案例 2:对象数组
  • 案例3:

案例一: Java返回一个整型(内置数据类型)数组

任何内置数据类型的数组是 integer、 字符、float、double 都可以返回,只需使用 return 语句记住上面列出的要点。

例子

Java
// Java Program to Illustrate Returning
// simple built-in arrays
 
// Importing input output classes
import java.io.*;
 
// Main class
class GFG {
 
    // Method 1
    // Main driver method
    public static void main(String[] args)
    {
        // An integer array storing the returned array
        // from the method
        int[] storage = methodReturningArray();
 
        // Printing the elements of the array
        for (int i = 0; i < storage.length; i++)
            System.out.print(storage[i] + " ");
    }
 
    // Method 2
    // Returning an integer array
    public static int[] methodReturningArray()
    {
        int[] sample = { 1, 2, 3, 4 };
 
        // Return statement of the method.
        return sample;
    }
}


Java
// Java Program to Illustrate Returning
// an array of objects in java
 
// Importing all input output classes
import java.io.*;
 
// Class 1
// Helper class
// Courses whose objects are returned as an array
class Courses {
 
    String name;
    int modules;
 
    // Constructor to instatiate class objects.
    public Courses(String n, int m)
    {
        // This keyword refers to current instance  itself
        this.name = n;
        this.modules = m;
    }
}
 
// Class 2
// Main class
class GFG {
 
    // Method 1
    // Main driver method
    public static void main(String[] args)
    {
        // Calling the method for returning an array of
        // objects of the Courses class.
        Courses[] sample = methodReturningArray();
 
        // Printing the returned array elements.
        for (int i = 0; i < sample.length; i++)
            System.out.print(sample[i].name + " - "
                             + sample[i].modules
                             + " modules\n");
    }
 
    // Method 2
    // Note that return type is an array
    public static Courses[] methodReturningArray()
    {
        // Declaring Array of objects of the Courses class
        Courses[] arr = new Courses[4];
 
        // Custom array of objects
        arr[0] = new Courses("Java", 31);
        arr[1] = new Courses("C++", 26);
        arr[2] = new Courses("DSA", 24);
        arr[3] = new Courses("DBMS", 12);
 
        // Statement to return an array of objects
        return arr;
    }
}


Java
// Java Program to Illustrate Returning
// Multi-dimensional Arrays
 
// Importing input output classes
import java.io.*;
 
// Main class
class GFG {
 
    // Method 1
    // Main driver method
    public static void main(String[] args)
    {
        // An integer 2D array storing the
        // returned array from the method
        int[][] storage = methodReturningArray();
 
        // Printing the elements of the array
        // using nested for loops
        for (int i = 0; i < storage.length; i++) {
            for (int j = 0; j < storage[0].length; j++)
 
                System.out.print(storage[i][j] + " ");
 
            System.out.println();
        }
    }
 
    // Method 2
    // Returning an integer array
    public static int[][] methodReturningArray()
    {
        // Custom 2D integer array
        int[][] sample
            = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
 
        // Return statement of the method
        return sample;
    }
}


输出
1 2 3 4 

案例二: Java返回对象数组

在返回内置数据类型的数组的情况下,这完全以类似的方式完成。



例子

Java

// Java Program to Illustrate Returning
// an array of objects in java
 
// Importing all input output classes
import java.io.*;
 
// Class 1
// Helper class
// Courses whose objects are returned as an array
class Courses {
 
    String name;
    int modules;
 
    // Constructor to instatiate class objects.
    public Courses(String n, int m)
    {
        // This keyword refers to current instance  itself
        this.name = n;
        this.modules = m;
    }
}
 
// Class 2
// Main class
class GFG {
 
    // Method 1
    // Main driver method
    public static void main(String[] args)
    {
        // Calling the method for returning an array of
        // objects of the Courses class.
        Courses[] sample = methodReturningArray();
 
        // Printing the returned array elements.
        for (int i = 0; i < sample.length; i++)
            System.out.print(sample[i].name + " - "
                             + sample[i].modules
                             + " modules\n");
    }
 
    // Method 2
    // Note that return type is an array
    public static Courses[] methodReturningArray()
    {
        // Declaring Array of objects of the Courses class
        Courses[] arr = new Courses[4];
 
        // Custom array of objects
        arr[0] = new Courses("Java", 31);
        arr[1] = new Courses("C++", 26);
        arr[2] = new Courses("DSA", 24);
        arr[3] = new Courses("DBMS", 12);
 
        // Statement to return an array of objects
        return arr;
    }
}
输出
Java - 31 modules
C++ - 26 modules
DSA - 24 modules
DBMS - 12 modules

案例 3:返回多维数组

Java的多维数组可以说是数组内部的数组。最简单的形式可以是二维数组。他们有他们的大小和根据他们的大小声明。下面演示了二维数组的返回,它具有与一维数组非常相似的方法。

例子

Java

// Java Program to Illustrate Returning
// Multi-dimensional Arrays
 
// Importing input output classes
import java.io.*;
 
// Main class
class GFG {
 
    // Method 1
    // Main driver method
    public static void main(String[] args)
    {
        // An integer 2D array storing the
        // returned array from the method
        int[][] storage = methodReturningArray();
 
        // Printing the elements of the array
        // using nested for loops
        for (int i = 0; i < storage.length; i++) {
            for (int j = 0; j < storage[0].length; j++)
 
                System.out.print(storage[i][j] + " ");
 
            System.out.println();
        }
    }
 
    // Method 2
    // Returning an integer array
    public static int[][] methodReturningArray()
    {
        // Custom 2D integer array
        int[][] sample
            = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
 
        // Return statement of the method
        return sample;
    }
}
输出
1 2 3 
4 5 6 
7 8 9