我一直在为即将到来的Java考试而学习,而且我很难把头放在2D阵列上。我掌握了一些基础知识,例如创建和初始化2D数组,但是当涉及到插入,删除甚至排序时,我会非常困惑。我的教授花了整整10分钟的时间介绍基础知识,但是在我们的考试中,我们希望知道如何创建2D对象数组,以及如何通过在数组中插入,删除和排序对象来操作数据。他让我们在考试中手写所有代码,因此不允许任何计算机协助完成该过程。我已经花了数小时在这里和其他站点上翻阅示例,但是我仍然觉得我不太了解这些材料,无法亲自编写所有代码并正确编写。
我的困惑主要源于通常用于通过2D数组移动的嵌套for
循环。我可以看到其他人是如何做到并复制它的,但是我仍然不理解为什么循环像他们那样工作。我敢肯定我在这里是少数派,但是无论出于什么原因,背后的逻辑使我完全迷失了。
以这个(尽管很差)为例,我一直在努力帮助自己理解2D数组(以及其余的考试材料)。假设您经营一家汽车经销店,并且想订购汽车以填补库存。该程序从顶级abstract
类开始,该类描述您一般销售的汽车(在我的情况下是奥迪)。该经销店提供4种Audi型号,分别为A4
,A6
,A8
和R8
。所有这些汽车(类)都从名为super
的Audi
继承方法。然后,我想创建一个2D数组来存储库存的汽车。这将在我定义的另一个类中使用,包括search()
,delete()
和sort()
的方法。我们称它为AudiDealership
。经销商每个模型只能容纳3个,因此阵列将类似于Audi[4][3]
。 A4填充第一行subscript 0
,A6填充subscript 1
,依此类推。我将如何设置for
循环以从正确的行中插入/删除?我显然不希望将A4插入应该容纳A6的行中,依此类推。
同样,我可以整天盯着代码并复制它,但是我想理解 为什么 / 如何循环像它们一样工作。如果这个话题看起来很琐碎或被打死了,我深表歉意,但是我在发布此主题之前所做的所有阅读使我像以前一样困惑。这个网站上的许多人都以自己的能力成为了出色的老师,因此我认为有人可以用我能理解的方式来解释这一点。我的教授在这方面没有任何帮助,所以我正在使用外部手段来尝试解决问题。我非常感谢您提前提出任何建议或解释:)
它有助于将2D数组视为保存其他数组的数组。例如Cars[0][5]
正在访问位于0的汽车阵列,而实际汽车位于该阵列的位置5。 Cars[1][5]
将访问位置1处的第二个汽车阵列,并在位置1的阵列中的5个位置找到汽车。此代码可以帮助您进一步了解它:
public class TwoDArray {
public static void main(String[] args)
{
Cars[][] cars; // declaring my 2D array.
cars = new Cars[4][]; // making the x axis 4 cars wide.
// now we will step along the x axis and create a Cars array in each slot.
for ( int a = 0; a < cars.length; a++) // cars.length = how wide.
{
cars[a] = new Cars[3]; // creating a new Cars[] in each slot of our 2D Car array @ position a.
//cars[a] = new 1D Cars array in slot a, length of 3.
}
// Note that we could have also created our array like this.
// Cars[][] cars = new Cars[4][3];
for ( int x = 0; x < cars.length; x++) //stepping along the x axis. cars.length = how wide.
{ //every time we step thru x we will execute this next loop.
for ( int y = 0; y < cars[x].length; y++) // stepping along the y axis. cars[x].length = how long.
{ // this loop will cycle through the y axis each time we increment x
cars[x][y] = new Cars( 2014, "someAudi", x + " " + y ); // creating a new car(s) @ x,y position.
}
}
// now to just print them.
for ( int x = 0; x < cars.length; x++) //stepping along the x axis again.
{
for ( int y = 0; y < cars[x].length; y++) // stepping along the y axis.
{
System.out.println(cars[x][y].getYear() +
" " + cars[x][y].getModel() +
" " + cars[x][y].getName() +
" " + cars[x][y].getManufacturer()); // the super method.
}
}
//INSERTION.
// To insert into your array, you simply need to provide the coordinates to insert the new Car(s) object.
// This next line will insert a new Car into the array at position 1 and the number 2 element of that array.
cars[1][2] = new Cars( 2014, "someAudi", "My Favorite Car!");
System.out.println(); // Just adding a space between outputs.
for ( Cars[] c: cars) //extracting each Cars array and name it c from the 2D Cars array named cars.
{ //basically stepping along the x axis and getting each array stored in x.
for ( Cars car: c) // Now we are stepping along the y axis.
{ // We are getting each individual Cars object and naming it car
// from each Cars[] named c from our first loop.
System.out.println(car.getYear() +
" " + car.getModel() +
" " + car.getName() +
" " + car.getManufacturer()); // the super method.
}
}
// NOTE* if you wish to insert a new element and do not have extra capacity then you will need to
// create a larger array @ cars[x]. cars[x] = new Cars[newSize];.
// DELETION.
// To delete an element you can just simply overwrite it.
// such as:
cars[1][1] = new Cars( 2014, "someAudi", "new Audi"); // Essentially we deleted and inserted a new object
// at position [1][1].
// If you just want to completely remove the element then you will need to update the size of the array.
// You can define a new array to hold the values of the old array minus the element that should be deleted.
Cars[] newArray = new Cars[cars[2].length - 1]; // We will use the array stored in cars[2] for this example.
// we set the length to one less to completely get rid of the
// old element.
int deleteThisPosition = 1; // We will use this variable to store the position that will be deleted from
// the array stored in cars[2].
int newArrayPosition = 0; // We will use this to increment our position in the new array along with `a`
// in the next for loop.
for ( int a = 0; a < cars[2].length; a++)
{
if ( a == deleteThisPosition) // if it reaches this position we will advance `a` and exclude it from
a++; // our new array.
newArray[newArrayPosition] = cars[2][a]; // we will store the value @ position `a` from the array in cars[2]
// into our newArray @ position `newArrayPosition`.
newArrayPosition++; // incrementing our variable to stay parallel with the array in cars[2].
}
//Now we can just assign the newArray to cars[2]. You will notice that Car `2 1` is no longer present.
cars[2] = newArray;
System.out.println(); // Just adding a space between outputs.
for ( Cars[] c: cars)
{
for ( Cars car: c)
{
System.out.println(car.getYear() +
" " + car.getModel() +
" " + car.getName() +
" " + car.getManufacturer()); // the super method.
}
}
}
}
这里是您示例中的其他类。
奥迪班:
public abstract class Audi {
public String getManufacturer() { return "Audi"; } // method from this super class.
}
汽车类别:
public class Cars extends Audi{ //extending Audi.
private String model;
private String name;
private int year;
Cars(int year, String model, String name)
{
this.year = year;
this.model = model;
this.name = name;
}
public String getName() { return name; }
public String getModel() { return model; }
public int getYear() { return year; }
}
如果运行代码,您会注意到汽车名称中的模式。
输出:
<< img src =“ https://image.soinside.com/eyJ1cmwiOiAiaHR0cHM6Ly9pLnN0YWNrLmltZ3VyLmNvbS9DTTdNby5wbmcifQ==” alt =“在此处输入图像描述”>
请注意每辆车名称中的模式,唯一列。它对应于我们如何逐步执行循环。我们从x开始,对于每个x,我们都遍历y。 x + " " + y
是我们在上面的代码中命名每辆汽车的方式。
Audi[4][3] cars = ... // your 2D array of all cars
如您所正确指定的,
[A4将填充第一行,下标0,A6填充下标1,等等。
翻译为cars[0]
保存Audi[] with A4 instances
,cars[1]
保存Audi[] with A6 instances
,等等。>
好,所以
Audi[] A4s = cars[0]; Audi[] A6s = cars[1]; ...
那么你可以这么说
Audi A4_1 = A4s[0]; Audi A4_2 = A4s[1]; Audi A4_3 = A4s[2]; ...
并为您拥有的每辆车重复一遍。但这是错误的方法。首先,我们概括性地访问每辆车。
如果要遍历每个模型阵列中的特定汽车,则需要有一个带索引的for循环,例如specificCarIndex
。循环遍历A4s
的数组将很简单:
for (int specificCarIndex = 0; specificCarIndex < 3; specificCarIndex++) { // Here A4s[specificCarIndex] contains an object of concrete Audi car of model A4 }
要遍历另一个模型(如A6)的数组,请执行相同的操作,将
A4s
替换为A6s
,依此类推。
现在我们需要归纳所有内容。
for (int carModelIndex = 0; carModelIndex < 4; carModelIndex++) { // Here cars[carModelIndex] holds an array of specific Audi model as you mentioned before }
[
cars[carModelIndex]
本质上是Audi[] A4s
,如果是carModelIndex == 0
,则是Audi[] A6s
,如果是carModelIndex == 1
,依此类推。
现在,我们知道如何访问每个奥迪模型的阵列,并且我们知道如何访问每个模型阵列中的各个汽车,我们将两者结合起来:
for (int carModelIndex = 0; carModelIndex < 4; carModelIndex++) {
for (int specificCarIndex = 0; specificCarIndex < 3; specificCarIndex++) {
// Here cars[carModelIndex][specificCarIndex] holds an object of type Audi which refers to a specific car
// If double index seems difficult, consider this:
// Audi[] audis = cars[carModelIndex] (array like A4s, A6s...)
// Audi audi = audis[specificCarIndex] (specific car)
// Just as in the examples before for-loops.
}
}
让我给你一个例子: