使用 4D 数组的 Java 程序

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5540024/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-30 11:36:17  来源:igfitidea点击:

Java Program using 4D array

javaarraysmultidimensional-arrayvariable-assignment

提问by Prathamesh

I'm a first year computer engineering student and I'm quite new here. I have been learning Java for the past three and a half months, and C++ for six months before that. My knowledge of Java is limited to defining and using own methods, absolute basics of object-oriented programming like use of static data members and member visibility.

我是计算机工程专业的一年级学生,我在这里很新。过去三个半月我一直在学习 Java,在此之前六个月我一直在学习 C++。我对 Java 的了解仅限于定义和使用自己的方法、面向对象编程的绝对基础,例如使用静态数据成员和成员可见性。

This afternoon, my computer programming prof taught us about multi-dimensional arrays in Java. About multi-dimensional arrays being simply arrays of arrays and so on. He mentioned that in nominal, educational programming, arrays beyond 2 dimensions are almost never used. Even 3D arrays are used only where absolutely essential, like carrying out scientific functions. This leaves next to zero use for 4D arrays as using them shows that "you're using the wrong datatype" in my prof's words.

今天下午,我的计算机编程教授教我们 Java 中的多维数组。关于多维数组只是数组的数组等等。他提到在名义上的教育编程中,几乎从不使用超过 2 维的数组。甚至 3D 阵列也仅在绝对必要的情况下使用,例如执行科学功能。这使得 4D 数组的使用接近于零,因为使用它们表明用我教授的话来说“您使用了错误的数据类型”。

However, I'd like to write a program in which the use of a 4D array, of any data type, primitive or otherwise, is justified. The program must not be as trivial as printing the elements of the array.

但是,我想编写一个程序,在该程序中使用任何数据类型、原始数据或其他数据类型的 4D 数组都是合理的。该程序不能像打印数组元素一样琐碎。

I have no idea where to begin, this is why I am posting this here. I'd like your suggestions. Relevant problem statements, algorithms, and bits and pieces of code are also welcome.

我不知道从哪里开始,这就是我在这里发布的原因。我想要你的建议。也欢迎提供相关的问题陈述、算法和零碎的代码。

Thank you.

谢谢你。

Edit: Forgot to mention, I have absolutely no idea about working with GUIs in Java, so please do not post ideas that implement GUIs.

编辑:忘了提,我完全不知道在 Java 中使用 GUI,所以请不要发布实现 GUI 的想法。

回答by Victor Sorokin

Ideas:

想法:

- Matrix multiplication and it's applications like finding shortest path in graphs - Solving of systems of equations - Cryptography -- many cryptoprotocols represent data or keys or theirs internal structures in a form of matrices. - Any algo on graphs represented as matrices

- 矩阵乘法及其应用,例如在图中寻找最短路径 - 求解方程组 - 密码学 - 许多加密协议以矩阵形式表示数据或密钥或其内部结构。- 以矩阵表示的图形上的任何算法

I must have been having some kind of fixation on matrices, sorry :)

我一定对矩阵有某种执着,抱歉:)

For 4D arrays one obvious thing I can think of is the representation of 3D environment changing in time, so 4th dimension represents time scale. Or any representation of 3D which have additional associated property placed in 4th dimension of array.

对于 4D 数组,我能想到的一件显而易见的事情是 3D 环境随时间变化的表示,因此第 4 维表示时间尺度。或者任何在数组的第 4 维中具有附加关联属性的 3D 表示。

回答by RoflcoptrException

You could create a Sodoku hypercube with 4 dimensions and stores the numbers the user enters into a 4dimensional int array.

您可以创建一个具有 4 维的 Sodoku 超立方体,并将用户输入的数字存储到 4 维 int 数组中。

回答by Ishtar

One use could be applying dynamic programmingto a function that takes 4 integer parameters f(int x,int y,int z,int w). To avoid calling this expensive function over and over again, you can cache the results in a 4D array, results[x][y][z][w]=f(x,y,z,w);.

一种用途是将动态规划应用到采用 4 个整数参数的函数f(int x,int y,int z,int w)。为了避免一遍又一遍地调用这个昂贵的函数,您可以将结果缓存在 4D 数组中results[x][y][z][w]=f(x,y,z,w);

Now you just have to find an expensive integer function with arity of 4, oh, and a need for calculating it often...

现在你只需要找到一个昂贵的整数函数,它的 arity 为 4,哦,并且需要经常计算它......

回答by EightyOne Unite

Just to back him up,..your prof is quite right. I'm afraid I might be physically violent to anyone using a 4D+ array in production code.

只是为了支持他,..你的教授是对的。恐怕我可能会对在生产代码中使用 4D+ 数组的任何人产生身体暴力。

It's kindacool to be able to go into greater than 3 dimensions as an educational exercise but for real work it makes things waytoo complicated because we don't really have much comprehension of structures with greater than 3 dimensions.

还挺酷才能够进入大于3点的尺寸作为教育工作,但真正的工作是使事情的方式太复杂,因为我们并不真正具有大于3名维结构的多理解。

The reason it's difficult to come up with a practical use for 4D+ arrays is because there is (almost) nothing that complicated in the real world to model.

很难想出 4D+ 数组的实际用途的原因是因为在现实世界中(几乎)没有任何复杂的模型可以建模。

You could look into modelling something like a tesseract, which is (in layman's terms ) a 4D cube or as Victor suggests use the 4th dimension to model constant time.

你可以考虑建模类似tesseract 的东西,它是(用外行人的术语)一个 4D 立方体,或者像 Victor 建议的那样使用第 4 维来建模恒定时间。

HTH

HTH

回答by Elliander

There are many possible uses. As others have said, you can model a hypercube or something that makes use of a hypercube as well as modeling a change over time. However, there are many other possible uses.

有许多可能的用途。正如其他人所说,您可以对超立方体或利用超立方体的东西进行建模,也可以对随时间的变化进行建模。但是,还有许多其他可能的用途。

For example, one of the theoretical simulation models of our universe uses 11th dimensional physics. You can write a program to model what these assumed physics would look like. The user would only be able to see a 3-dimensional space which definitely limits usability, but the 4th dimensional coordinate could act like the changing of a channel allowing the user to change their perspective. If a 4th dimensional explosion occurs, for example, you might even want a 5th dimensional array so that you can model what it looks like in each connected 3-dimensional space as well as how it looks in each frame of time.

例如,我们宇宙的理论模拟模型之一使用了第 11 维物理。您可以编写一个程序来模拟这些假设的物理现象。用户只能看到一个 3 维空间,这肯定会限制可用性,但 4 维坐标可以充当频道的改变,允许用户改变他们的视角。例如,如果发生 4 维爆炸,您甚至可能需要一个 5 维数组,以便您可以对它在每个连接的 3 维空间中的外观以及在每个时间帧中的外观进行建模。

To take a step away from the scientific, think about an MMORPG. Today many of those games uses "instanced" locations which means that a copy of a given zone is created exclusively for the use of a given group of players so to prevent lag. If this "instanced" concept was given a 4th dimensional coordinate and it allows players to shift their position across instances it could effectively allow all server worlds to be merged together while allowing the players a great deal of control over where they go while decreasing cost.

要远离科学,请考虑 MMORPG。今天,这些游戏中的许多使用“实例化”位置,这意味着给定区域的副本是专门为给定的一组玩家创建的,以防止滞后。如果这个“实例化”概念被赋予一个第 4 维坐标,并且它允许玩家在实例之间移动他们的位置,那么它可以有效地允许所有服务器世界合并在一起,同时允许玩家在降低成本的同时对他们的去向进行大量控制。

Of course, your question wants to know about ideas without using a GUI. That's a bit more difficult because you are working in a 2D environment. One real application would be Calculus. We have 3D graphing calculators, but for higher dimensions you pretty much have to do it by hand. A pogram that aims to solve these calculations for you might not be able to properly display the information, but you can certainly calculate it. Also, when hologaphic interfaces become a widespread reality it may be possible to represent a hypercube graph in 3D making such a program useful.

当然,您的问题想在不使用 GUI 的情况下了解想法。这有点困难,因为您在 2D 环境中工作。一个真正的应用是微积分。我们有 3D 图形计算器,但对于更高的维度,您几乎必须手动完成。旨在为您解决这些计算的 pogram 可能无法正确显示信息,但您当然可以计算它。此外,当全息接口成为普遍存在的现实时,有可能以 3D 形式表示超立方图,从而使此类程序变得有用。

You might be able to write a text based board game where the position of pieces is represented with text. You can add dimensions and game rules to use them.

您或许可以编写一个基于文本的棋盘游戏,其中棋子的位置用文本表示。您可以添加维度和游戏规则以使用它们。

The simplest idea I could give you is a save state system. At each interval the program in memory is copied and stored into a file. It's coordinate is it's position in time. At face value you may not need a 4D array to handle this, but suppose the program you were saving states of used a 3D array. You could set it up to represent each saved state as a position in time that you can make use of and then view the change in time.

我能给你的最简单的想法是保存状态系统。在每个时间间隔,内存中的程序都会被复制并存储到一个文件中。它的坐标是它在时间上的位置。从表面上看,您可能不需要 4D 数组来处理这个问题,但假设您正在保存使用 3D 数组的状态的程序。您可以将其设置为将每个保存的状态表示为您可以使用的时间位置,然后查看时间更改。

回答by l?ōsêr xxx

Being a physics student we have only 3 dimension of space but we have a 4th dimension which is time. So thinking in that way we can think of an array of any dimension(1D or 2D or 3D) whose values differ with time or an array which keeps the record of every array whose values changed with time. It seems to be quite known to us. For example the "ATTENDANCE REGISTER" which we usually have in our classroom.

作为一名物理学生,我们只有空间的 3 维,但我们有 4 维,即时间。因此,以这种方式思考,我们可以考虑其值随时间变化的任何维度(1D 或 2D 或 3D)的数组,或者保留其值随时间变化的每个数组的记录的数组。对我们来说似乎很熟悉。例如我们教室里通常有的“出勤登记表”。

This is my view to it.

这是我对它的看法。

That's it. Enjoy :-)

而已。享受 :-)

回答by keepitreall89

I'm not sure what specifically you could do with this, because I just started thinking about it. But you could possibly use a 4D array for some sort of basic physics simulation, like modeling a projectile flight involving some wind values and what not. That just came to mind because the term 4D always brings to mind that the "position" of any object is 4 values, with time as the 4th.

我不确定你可以用它做什么,因为我刚刚开始考虑它。但是您可能会使用 4D 数组进行某种基本的物理模拟,例如对涉及某些风值的抛射体飞行进行建模,以及其他不涉及的东西。之所以想到这一点,是因为术语 4D 总是让人想起任何对象的“位置”是 4 个值,时间是第 4 个值。

回答by Igor F.

To give a concrete example for the Ishtar'sanswer: Four-string alignment. To compute optimal two-string alignment, you write one string along one (say, horizontal) axis of a 2D-array (a matrix!) and the other one along the other array. The array is filled with edit costs, and the optimal alignment of the two strings is the one which produces the lowest cost, associated with a path through the matrix. A common way of finding such a path is by the above mentioned dynamic programming. You can look up 'Levenshtein distance' or 'edit distance' for technical details.

Ishtar 的回答举一个具体的例子:四弦对齐。要计算最佳的两字符串对齐方式,您可以沿着二维数组(矩阵!)的一个(比如水平)轴编写一个字符串,并沿着另一个数组编写另一个字符串。该数组充满了编辑成本,两个字符串的最佳对齐是产生最低成本的字符串,与通过矩阵的路径相关联。找到这种路径的常用方法是通过上面提到的动态规划。您可以在“Levenshtein distance”或“edit distance”中查找技术细节。

The basic idea can be expanded to any number of strings. For four strings you'd need a four-dimensional array, to write each string along one of the dimensions.

基本思想可以扩展到任意数量的字符串。对于四个字符串,您需要一个四维数组,以沿其中一个维度编写每个字符串。

In practice, however, multiple string alignment is not done this way, for at least two reasons:

然而,在实践中,多字符串对齐并不是这样完成的,至少有两个原因:

  1. Lack of flexibility: Why would you need to align exactlyfour strings??? In computational molecular biology, for example, you might wish to align many strings (think of DNA sequences), and their number is not known in advance, but it is seldom four. You program would be useful for a very limited class of problems.

  2. Computational complexity, in space and time. The requirements are exponential in the number of dimensions, making the approach impractical for most real-world purposes. Besides, most of the entries in such multi-dimensional array would lie on such suboptimal paths, which are never even touched, so that storing them would be simply waste of space.

  1. 缺乏灵活性:为什么需要精确对齐四个字符串???例如,在计算分子生物学中,您可能希望对齐许多字符串(想想 DNA 序列),它们的数量事先并不知道,但很少有四个。您的程序对非常有限的一类问题很有用。

  2. 空间和时间上的计算复杂性。要求在维度数量上呈指数级增长,这使得该方法对于大多数现实世界的目的而言是不切实际的。此外,这种多维数组中的大多数条目都位于这种次优路径上,甚至永远不会被触及,因此存储它们只会浪费空间。

So, for all practical purposes, I believe your professor was right.

因此,出于所有实际目的,我相信您的教授是对的。