如何在Java中操作2D数组?
我一直在为即将到来的Java考试而学习,我很难对2D数组了如指掌。我已经掌握了一些基本知识,比如创建和初始化2D数组,但是当涉及到插入、删除甚至排序时,我会非常困惑。我的教授花了10分钟复习基础知识,但在我们的考试中,我们希望知道如何创建一个2D对象数组,并通过插入、删除和排序数组中的对象来操作数据。他让我们在考试中手工编写所有代码,因此不允许计算机协助这一过程。我花了数小时在这里和其他网站上反复讨论示例,但我仍然觉得我对材料的理解不够好,无法手工编写所有代码并使其正确 我的困惑主要源于嵌套的如何在Java中操作2D数组?,java,for-loop,multidimensional-array,Java,For Loop,Multidimensional Array,我一直在为即将到来的Java考试而学习,我很难对2D数组了如指掌。我已经掌握了一些基本知识,比如创建和初始化2D数组,但是当涉及到插入、删除甚至排序时,我会非常困惑。我的教授花了10分钟复习基础知识,但在我们的考试中,我们希望知道如何创建一个2D对象数组,并通过插入、删除和排序数组中的对象来操作数据。他让我们在考试中手工编写所有代码,因此不允许计算机协助这一过程。我花了数小时在这里和其他网站上反复讨论示例,但我仍然觉得我对材料的理解不够好,无法手工编写所有代码并使其正确 我的困惑主要源于嵌套的f
for
循环,通常用于在二维数组中移动。我可以看到其他人是如何做的,也可以复制,但我仍然不明白为什么循环会像他们那样工作。我相信我在这里是少数,但不管出于什么原因,它背后的逻辑让我完全迷失了
拿这个(尽管很差)例子来说,我一直在努力帮助自己理解2D数组(以及其他考试材料)。假设你经营一家汽车经销商,你想订购汽车来补充库存。该程序以一个顶级的抽象类开始,该类描述了您一般销售的汽车(在我的例子中是奥迪)。该经销商提供4款奥迪车型,一款A4
,A6
,A8
,以及R8
。所有这些汽车(类)都继承了名为奥迪的super
的方法。然后我想创建一个2D数组来存储库存中的汽车。这将在我定义的另一个类中使用,包括search()
、delete()
和sort()
的方法。让我们称之为audidership
。经销商每款车型只能容纳3辆,因此阵列类似于奥迪[4][3]
。A4将填充第一行,下标0
,A6填充下标1
,等等。如何设置循环的,以便插入/删除正确的行?很明显,我不希望将A4插入到一行中,该行应该包含A6,以此类推
同样,我可以整天盯着代码并复制它,但我想理解循环为什么/如何工作。如果这个话题看起来很琐碎或者被打死了,我很抱歉,但是我在发布之前读过的所有文章都让我和以前一样困惑。在这个网站上,你们中的许多人都是很棒的老师,所以我想有人可以用我能理解的方式来解释这一点。我的教授在这方面没有任何帮助,所以我正在使用外部手段来尝试解决问题。我非常感谢您事先提供的任何建议或解释:)让我举个例子:
StringBuilder output = new StringBuilder();
for (int i = 0; i < table.length; i++) {
for (int j = 0; j < table[i].length; j++) {
output.append(table[i][j]);
}
output.append("\n");
}
StringBuilder输出=新建StringBuilder();
对于(int i=0;i
这将是循环通过二维数组的代码
代码直接进入第一行。这是i值。
由于要循环遍历行的每一列,
您需要知道行的长度才能循环通过它
因此,从第一行开始,一直到最后一行,即表[i].length。
在第一行中,第一个长度为表[0]。长度。
这个长度是3,因为每行有3列
当我们现在完成第一行的循环时,我们进入第二行。
通过进入第二排,我增加了1。
所以我们的指针现在显示在第2行(表[i]中的i),长度现在是1,因为它总是从0开始),依此类推
我们的指针穿过每一行,这就是它的工作原理。
每增加一行,i的指针增加1(这是for循环中的i++)
在一行中每增加一列,j增加,i保持不变。
i仅在您输入新行时更改。
而j通过输入新列而改变
希望这有帮助;)
编辑:
另一个例子:
如果要获取第4行第3列的值:
您想要的值在这里:表[3][2]代码>
记住数组总是从0开始计数。:)
正如你正确指出的
A4将填充第一行,下标0,A6填充下标1等
这转化为汽车[0]
持有奥迪[]和A4车型
,汽车[1]
持有奥迪[]和A6车型
等
好的,那么
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
}
汽车[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.
}
}
for(int-carModelIndex=0;carModelIndex<4;carModelIndex++){
对于(int specificCarIndex=0;specificCarIndex<3;specificCarIndex++){
//在这里,cars[carModelIndex][specificCarIndex]持有一个奥迪类型的对象,它指的是一辆特定的汽车
如果双指标似乎困难,请考虑如下:
//奥迪[]奥迪=汽车[卡莫德林
for (int carModelIndex = 0; carModelIndex < 4; carModelIndex++) {
// Here cars[carModelIndex] holds an array of specific Audi model as you mentioned before
}
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.
}
}
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; }
}