在矩阵中插入和删除元素

发布于 2024-08-23 12:53:42 字数 659 浏览 3 评论 0原文

我有一个作业,其中有要求以下实现的问题:

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

美男兮 2024-08-30 12:53:42
|3    6|
|null 8|
|2    5|

|3    6|
|0    8|
|2    5|

|3    6|
|2    8|

或 许多其他形式表明自己可以作为替代方案。我认为你必须决定你被要求实施什么,然后实施它。

|3    6|
|null 8|
|2    5|

or

|3    6|
|0    8|
|2    5|

or

|3    6|
|2    8|

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.

高速公鹿 2024-08-30 12:53:42

我不知道你使用的是哪种语言,但它应该是这样的:

  • 插入一个新行,其中 i = newSizeOnX 为空值,
  • 直到我到达 x (你的行),随着 i 减少,复制
    行 (m[i+1]=[i])
  • 然后您只需在位置 m[x][y] 插入对象并填充其余部分
    m[x][y'](所有 y'!= y)

I don't know which language are you using, but it should be something like:

  • Insert a new row with empty values
  • for i = newSizeOnX until i reach x (your row), with i decreasing, copy
    the row (m[i+1]=[i])
  • then you just insert object in the position m[x][y] and fill the rest
    m[x][y'] (all the y' != y)
孤星 2024-08-30 12:53:42

在某种程度上,这是一个规范问题。

在正常系统中,您可以:

  • 决定将这些情况视为禁止的异常,如果发生则返回错误或警告
  • 定义特定的预定行为,该行为与您实现的所有功能一致,并明确记录

例如, - 用零或非数字填充矩阵是处理第一个问题的一种典型方法。 Matlab 默认情况下是如何处理这个问题的?

它还取决于应用程序。例如,如果您正在处理图像,那么在大多数情况下您很可能不想超过原始图像大小。如果您正在使用声音,默认情况可能是在扩展数据序列时插入静音。什么对您的应用最有意义?

Up to a certain point, this is a question of specifications.

In a normal system, you could either:

  • decide to treat those cases as forbidden exceptions, and return an error or a warning if it happens
  • define a specific predetermined behavior, that is consistent for all functions you implement, and clearly documented

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?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文