New*_*bie 1 java arrays multidimensional-array
我正在用 Java 完成一项作业,并有一个 2D 数组。我已经创建了多个类,目前我正在每个类中创建数组,并且想知道是否有一种简单的方法可以在整个程序中仅创建一次数组并在不同的类中调用它。我已经创建了一个 2D 数组,但我不想使用 ARRAYLIST (据我所知,当没有固定大小的数组时使用 ARRAYLIST,但就我而言,我知道我的 ARRAY 是 5x5)。
这是我的代码的基本示例:
public class accounts{
static int option=0;
static String[][] resultCard = new String[][]{
{ "", "A", "B", "C", "D", "E"},
{ "Account1", "1", "2","3","4","5" },
{ "Account2", "6", "7", "8", "9", "10"},
{ "Account3", "11", "12", "13", "14", "15"},
{ "Account4", "16", "17", "18", "19", "20"},
{ "Account5", "21", "22", "23", "24", "25"}
};
public static void method1() {
//I have entered some code here
Run Code Online (Sandbox Code Playgroud)
}
然后我创建了另一个类,如下所示:
public class customers{
static int option=0;
static String[][] resultCard = new String[][]{
{ "", "A", "B", "C", "D", "E"},
{ "Account1", "1", "2","3","4","5" },
{ "Account2", "6", "7", "8", "9", "10"},
{ "Account3", "11", "12", "13", "14", "15"},
{ "Account4", "16", "17", "18", "19", "20"},
{ "Account5", "21", "22", "23", "24", "25"}
};
public static void method2() {
//I have entered some code here
}
Run Code Online (Sandbox Code Playgroud)
我还有另一堂课,如下:
public class staff{
static int option=0;
static String[][] resultCard = new String[][]{
{ "", "A", "B", "C", "D", "E"},
{ "Account1", "1", "2","3","4","5" },
{ "Account2", "6", "7", "8", "9", "10"},
{ "Account3", "11", "12", "13", "14", "15"},
{ "Account4", "16", "17", "18", "19", "20"},
{ "Account5", "21", "22", "23", "24", "25"}
};
public static void method1() {
//I have entered some code here
}
public static void method1() {
//I have entered some code here
}
Run Code Online (Sandbox Code Playgroud)
我想知道,我是否可以只在 01 类中创建一个全局数组,并在不同的类中调用该数组,而不是在每个类中创建数组。
虽然其他答案可以很好地满足您的需求,但您永远不应该在可变(可更改)字段上使用public static
- 这意味着任何人都可以在应用程序中的任何位置更改该字段并导致未知的行为。如果添加修饰符,基元 ( int double float
) 不会改变 final
,但像数组这样的对象可以修改其内容 - 只有对象引用本身是常量。因此,该数组是可变的,因此您不应该使用public static
它。
看来您String[][]
几乎要表示一个表,其中的第一个元素是String[][]
标题,而其他每一行都是该表中的一个新行。因此,我们可以将您的内容String[][]
变成更惯用的内容。
我们可以让你的每一行都由String[][]
一个类的实例来表示。这样做有一些好处,但也就是说,它意味着您可以查看它并说“这是 X”,而不是“这是一个字符串数组”。
public class Account {
private final String name;
private final String a, b, c, d, e;
public Account(String name, String a, String b, String c, String d, String e) {
this.a = a;
this.b = b;
this.c = c;
this.d = d;
this.e = e;
this.name = name;
}
public string getName() { return this.name; }
public string getA() { return this.a; }
public string getB() { return this.b; }
public string getC() { return this.c; }
public string getD() { return this.d; }
public string getE() { return this.e; }
}
Run Code Online (Sandbox Code Playgroud)
这将这些字段封装在对象内。这很棒,因为这意味着首先您可以控制字段的分配位置以及可以更改的位置。
现在,我们可以将您的String[][]
变成Account[]
. 如上所述,直接共享数组是不好的做法。我们如何解决这个问题?好吧,我们可以创建另一个类来控制这些帐户的生命周期。由于它是一个数据存储,我们将其称为存储库(注意:这称为存储库模式)。
public class AccountRepository {
private final Collection<Account> accounts;
public AccountRepository(Collection<Account> accounts) {
this.accounts = accounts;
}
public Account getByName(String accountName) {
// Note that this is very inefficient, and I would not recommend using it in real code
for(Account account : this.accounts) {
if(account.getName().equalsIgnoreCase(accountName)) {
return account;
}
}
// Note: Returning null vs throwing exception is out of scope of this example.
return null;
}
public Iterator<Account> getAccounts() {
return this.accounts.getIterator();
}
}
Run Code Online (Sandbox Code Playgroud)
现在,这为我们提供了一个数据存储,可以用于存储任意数量的Account
s,并且可能来自任何源 - 我们不需要担心传递数组,因为我们可以传递一个实例,并且最好一点是我们给感兴趣的各方一个Iterator<Account>
而不是一个Account[]
。这很棒的原因是因为Iterators
它们是不可变的——你无法改变它们。看到这一点,现在,我们可以创建您的其他两个类(员工和客户 - 他们的名称和目的是这个问题之外辩论的另一个主题)并告诉他们他们不需要担心String[][]
- 他们可以直接采取他们的构造函数中的一个实例AccountRepository
。
public class Customers {
private final AccountRepository accountRepository;
public Customers(AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}
public void method1() {
// do something
}
}
public class Staff {
private final AccountRepository accountRepository;
public Staff(AccountRepository accountRepository) {
this.accountRepository = accountRepository;
}
public void method1() {
// do something
}
}
Run Code Online (Sandbox Code Playgroud)
这使您的代码更加灵活、可维护且易于遵循。您的Staff
和Customers
实例将需要AccountRepository
创建一个 ,它可以是相同的,并且这些类将调用 上的方法来AccountRepository
安全地访问数据,而没有机会更改数据。
Account[] accounts = new Account[] {
new Account("Account1", "1", "2", "3", "4", "5"),
// the rest of the accounts
};
AccountRepository repository = new AccountRepository(Arrays.asList(accounts));
Customers customers = new Customers(repository);
Staff staff = new Staff(repository);
Run Code Online (Sandbox Code Playgroud)
在我看来,您应该始终尽量避免“静态”属性或字段 - 其使用在大多数情况下都表明代码有异味。
希望这对您有所帮助并且不会让您感到困惑。