gpt4 book ai didi

java - 如何在 Java 中操作二维数组?

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:12:09 25 4
gpt4 key购买 nike

我一直在为即将到来的 Java 考试而学习,但我很难全神贯注于二维数组。我掌握了一些基础知识,例如创建和初始化二维数组,但在插入、删除甚至排序时,我感到很困惑。我的教授花了 10 分钟的时间复习基础知识,但在我们的考试中,我们应该知道如何创建一个二维对象数组,以及如何通过插入、删除和排序数组中的对象来操作数据。他让我们在考试中手写所有的代码,所以不允许计算机在这个过程中提供帮助。我在这里和其他网站上花了几个小时来研究示例,但我仍然觉得我对这些 Material 的理解还不够深入,无法亲手编写所有代码并使其正确无误。

我的困惑主要源于嵌套的 for 循环,通常用于在二维数组中移动。我可以看到其他人是如何做的并复制它,但我仍然不明白为什么循环会像他们那样工作。我确定我在这里是少数,但无论出于何种原因,它背后的逻辑让我完全迷失了。

以我一直致力于帮助自己理解二维数组(以及其余考试 Material )的这个(尽管很差)示例为例。假设你经营一家汽车经销店,你想订购汽车来填补你的库存。该程序以顶级 abstract 类开始,该类描述了您一般销售的汽车(在我的例子中是 Audi)。该经销商提供 4 款奥迪车型,分别是 A4A6A8R8。所有这些汽车(类)都继承了 super 的方法,名为 Audi。然后我想创建一个二维数组来存储库存中的汽车。这将在我定义的另一个类中使用,包括 search()delete()sort() 的方法。我们称它为 AudiDealership。经销商只能持有每个型号的 3 个,因此数组类似于 Audi[4][3]。 A4 将填充第一行,下标 0,A6 填充 下标 1,等等。我将如何设置 for 循环以从正确的行插入/删除?我显然不希望将 A4 插入应该容纳 A6 的行中,依此类推。

同样,我可以整天盯着代码并复制它,但我想理解 为什么/如何循环在它们运行时如何工作做。如果这个话题看起来微不足道或被打死,我深表歉意,但我在发布这篇文章之前所做的所有阅读让我和以前一样困惑。这个网站上的许多人本身就是出色的老师,所以我认为有人可以用我能理解的方式解释这一点。我的教授在这方面没有任何帮助,所以我正在使用外部手段来尝试解决问题。我非常感谢任何提前的建议或解释:)

最佳答案

将二维数组视为包含其他数组的数组会有所帮助。例如 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; }

}

如果您运行代码,您会注意到汽车名称中的模式。

输出:

enter image description here

注意每辆车名称中的模式,即唯一列。它对应于我们如何逐步完成我们的循环。我们从 x 开始,对于每个 x,我们循环遍历 y。 x + ""+ y 是我们在上面的代码中命名每辆车的方式。

关于java - 如何在 Java 中操作二维数组?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26299935/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com