在矩阵中插入和删除元素
我有一个作业,其中有要求以下实现的问题:
insertAtRanks(Integer x, Integer y, Object o): insert a new element to be stored at position (x,y)
并且
Object removeAtRanks(Integer x, Integer y): remove and return the element at position (x,y)
它已经要求实现replaceAtRanks,其中我必须用参数替换位置内的元素。
所以我假设在插入和删除元素时,矩阵的大小会增加和减少,但我的问题是如何?
例如,
| 3 6|
| 2 5|
如果我必须在位置 (1,1) 处插入数字 8,会发生以下情况吗?
| 3 6|
| 2 8|
| null 5|
如果我必须删除 (1,1) 处的元素,它会返回到吗?
| 3 6|
| 2 5|
编辑:
我使用 Java 来实现,并且使用类的二维数组来表示矩阵。
I have an assignment where I have questions that ask for the following implementations:
insertAtRanks(Integer x, Integer y, Object o): insert a new element to be stored at position (x,y)
and
Object removeAtRanks(Integer x, Integer y): remove and return the element at position (x,y)
It already asked for the implementation of replaceAtRanks where I had to replace the element inside a position with a parameter.
So what I assume when inserting and removing elements, the matrix will increase and decrease in size, but my question is how?
For example
| 3 6|
| 2 5|
If I had to do an insert number 8 at position (1,1) will the following happen?
| 3 6|
| 2 8|
| null 5|
And if I had to remove the element at (1,1) afterwards will it go back to?
| 3 6|
| 2 5|
Edit:
I am using Java for the implementation, and I am using a 2 dimensional array of classes to represent the matrix.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
或
或
或 许多其他形式表明自己可以作为替代方案。我认为你必须决定你被要求实施什么,然后实施它。
or
or
or many other forms suggest themselves as alternatives. I think you have to decide what you have been asked to implement, and then implement it.
我不知道你使用的是哪种语言,但它应该是这样的:
行 (m[i+1]=[i])
m[x][y'](所有 y'!= y)
I don't know which language are you using, but it should be something like:
the row (m[i+1]=[i])
m[x][y'] (all the y' != y)
在某种程度上,这是一个规范问题。
在正常系统中,您可以:
例如, - 用零或非数字填充矩阵是处理第一个问题的一种典型方法。 Matlab 默认情况下是如何处理这个问题的?
它还取决于应用程序。例如,如果您正在处理图像,那么在大多数情况下您很可能不想超过原始图像大小。如果您正在使用声音,默认情况可能是在扩展数据序列时插入静音。什么对您的应用最有意义?
Up to a certain point, this is a question of specifications.
In a normal system, you could either:
For instance, pre-filling the matrix with zeroes or not-a-number is one typical way of handling your first problem. How is Matlab handling this by default?
It also depends on the application. For instance, if you are working with images, chances are you don't want to exceed the original image size in most cases. If you are working with sound, the default case is probably to insert silence when extending a data sequence. What makes the most sense for YOUR application?