简体   繁体   English

数组中 1:st 索引上的多个值

[英]Multiple values on 1:st index in array

I'm trying to add lines in a menu.我正在尝试在菜单中添加行。 The object I try to change is an Core.Array.我尝试更改的 object 是 Core.Array。 And it's named groupSizes.它被命名为 groupSizes。

I'm trying to do like this:我正在尝试这样做:

((((UI.Menu current builder menuAt: #baseMenu) atNameKey: #menu6axisSpecialTool) submenu menuItems at: 5) submenu) groupSizes at: 1 put: #(4 1 9 4)

But that just puts a new array in side the array (logical).但这只是将一个新数组放在数组旁边(逻辑上)。 And "add:" don't work.并且“添加:”不起作用。 I think I wish to achive something like this:我想我希望实现这样的目标:

((((UI.Menu current builder menuAt: #baseMenu) atNameKey: #menu6axisSpecialTool) submenu menuItems at: 5) submenu) at: groupSizes put: #(4 1 9 4)

But that don't work either.但这也行不通。 I can inspect the object and manually change the array from (18) to my desired (4 1 9 4).我可以检查 object 并手动将数组从 (18) 更改为我想要的 (4 1 9 4)。 To confim that the change will achive what I wish.确认更改将实现我的愿望。

Oh, this will be a change that is applied to the image by fileIn from an external.st-file.哦,这将是由外部.st 文件中的 fileIn 应用于图像的更改。 Thats why I can't change how it's created, (that and the fact that I barely know what I'm doing.) and wish to manipulate the values inside the array.这就是为什么我无法更改它的创建方式(以及我几乎不知道自己在做什么的事实。)并希望操纵数组中的值。

I'm 100% green on smalltalk.我在 smalltalk 上是 100% 的绿色。 So I attached a picture that might say more then my words.所以我附上了一张可能比我的话更能说明问题的图片。 在此处输入图像描述

Any idéas of how to change the value of that array?关于如何更改该数组的值的任何想法?

On closer examination, this is more likely what i wish to do:经过仔细检查,这更有可能是我希望做的:

((((UI.Menu current builder menuAt: #baseMenu) atNameKey: #menu6axisSpecialTool) submenu menuItems at: 5) submenu) groupSizes at: 1 put: 4.
((((UI.Menu current builder menuAt: #baseMenu) atNameKey: #menu6axisSpecialTool) submenu menuItems at: 5) submenu) groupSizes at: 2 put: 1.
((((UI.Menu current builder menuAt: #baseMenu) atNameKey: #menu6axisSpecialTool) submenu menuItems at: 5) submenu) groupSizes at: 3 put: 9.
((((UI.Menu current builder menuAt: #baseMenu) atNameKey: #menu6axisSpecialTool) submenu menuItems at: 5) submenu) groupSizes at: 4 put: 4.

But the array don't have index 2-4, how can I increase the number of index:es?但是数组没有索引2-4,我怎样才能增加索引的数量:es?

You shouldn't be so rude to your Objects.你不应该对你的对象如此粗鲁。 What you try to achieve is to modify a menu, so what you should be doing is kindly ask the menu to perform the changes or create a new menu instead.您尝试实现的是修改菜单,因此您应该做的是请菜单执行更改或创建新菜单。 What you shouldn't be doing is modify the data that the menu operates on.您不应该做的是修改菜单操作的数据。


But first things first: You ask how you can add to an Array and the answer is simple: You can't.但首先要做的事情是:你问如何添加到数组中,答案很简单:你不能。 Arrays are meant to be created at a fixed size and not change their size ever: If you create an Array via: Array new: 4 you can access the slots 1 to 4 and put anything in it that you like. Arrays 旨在以固定大小创建并且永远不会更改其大小:如果您通过以下方式创建数组: Array new: 4您可以访问插槽 1 到 4 并在其中放入您喜欢的任何内容。 But if you try to do anArray add: 1 you'll get an Error via: self shouldNotImplement .但是如果你尝试做anArray add: 1你会得到一个错误 via: self shouldNotImplement Now the comment of the method #add: is along the lines of "yeah, you really shouldn't" , which is absolutely not helpful.现在方法#add:的注释是“是的,你真的不应该” ,这绝对没有帮助。 Even the class-comment doesn't say a word about "why" .甚至课堂评论也没有说“为什么” Among other things, Arrays are used for passing data to/from the VirtualMachine, where it's easier to keep things simple instead of having complicated objects (dealing with growth is pretty complicated stuff).除其他外,Arrays 用于向/从 VirtualMachine 传递数据,其中更容易保持简单而不是拥有复杂的对象(处理增长是非常复杂的事情)。

If you need a collection that can grow and behave a lot like Arrays, use OrderedCollection .如果您需要一个可以增长且行为类似于 Arrays 的集合,请使用OrderedCollection They can grow, they can shrink, you can add to them or remove from them.它们可以增长,它们可以缩小,您可以添加它们或从中删除它们。 OrderedCollection is basically the go-to Collection for when you need a list of things. OrderedCollection基本上是您需要列表时的首选集合。

For more information on Collections, you should have a look at the BasicLibraries.pdf in your VisualWorks' installation's doc folder.有关 Collections 的更多信息,您应该查看 VisualWorks 安装的doc文件夹中的 BasicLibraries.pdf。 Chapter 1 explains the various Collection subclasses.第 1 章解释了各种Collection子类。 The most common classes that you may be using are: OrderedCollection , Dictionary and Set .您可能使用的最常见的类是: OrderedCollectionDictionarySet Set you'll use almost never, Dictionary a bit more and OrderedCollection a lot more. Most of the other Collections are rarely needed. It's good to have an overview of what's there, but you really don't need most of them. Set你几乎从不使用, Dictionary a bit more and OrderedCollection a lot more. Most of the other Collections are rarely needed. It's good to have an overview of what's there, but you really don't need most of them. a lot more. Most of the other Collections are rarely needed. It's good to have an overview of what's there, but you really don't need most of them. SortedCollection used to be used more frequently back in the days. Nowadays it's easier to just sort any collection via过去,SortedCollection used to be used more frequently back in the days. Nowadays it's easier to just sort any collection via used to be used more frequently back in the days. Nowadays it's easier to just sort any collection via #sorted:` and not bother with SortedCollection at all. used to be used more frequently back in the days. Nowadays it's easier to just sort any collection via而根本不用理会SortedCollection。

So to answer your Question:所以回答你的问题:

Any idéas of how to change the value of that array?关于如何更改该数组的值的任何想法?

The answer is simply: Don't even try.答案很简单:不要尝试。


To answer your actual question:要回答您的实际问题:

I'm trying to add lines in a menu.我正在尝试在菜单中添加行。

There're other things you can do:您还可以做其他事情:

  1. Menus are resources, so there's probably a method that describes the menu somewhere on class-side of an ApplicationModel .菜单是资源,因此可能有一种方法可以在ApplicationModel的类侧某处描述菜单。 You can edit the menu with the menu editor.您可以使用菜单编辑器编辑菜单。

  2. Menus are hierarchies, so a Menu object is both the root of the menu, but also the object that's stored in a MenuItem 's submenu .菜单是层次结构,因此Menu object 既是菜单的根,也是存储在MenuItemsubmenu中的 object 。 If you want to replace a menu item's submenu, just tell it to: aMenuItem submenu: myNewMenu .如果要替换菜单项的子菜单,只需告诉它: aMenuItem submenu: myNewMenu So you don't need to actually modify the sub-sub-sub menu that you try to modify.因此,您不需要实际修改您尝试修改的子子菜单。 You can also create a new Menu object and just replace the sub-menu.您也可以创建一个新Menu object 并替换子菜单。 When you create a Menu you can use the following code:创建Menu时,您可以使用以下代码:

    myNewMenu:= Menu new addItem: firstItem; myNewMenu:= 菜单新 addItem: firstItem; addItem: secondItem;添加项目:第二项目; addLine;添加线; addItem: thirdItem;添加项:第三项; yourself.你自己。 mySubmenu menu: myNewMenu. mySubmenu 菜单:myNewMenu。

So with #addItem: you can add your existing items to a new menu.因此,使用#addItem:您可以将现有项目添加到新菜单中。 And with #addLine you can tell the menu to create a new group and create a separator line.并且使用#addLine您可以告诉菜单创建一个新组并创建一个分隔线。

If you need in-depth information on how to deal with Menus, i'd suggest you look at the GUIDevGuide.pdf in your VisualWorks' installation's doc folder.如果您需要有关如何处理菜单的深入信息,我建议您查看 VisualWorks 安装的doc文件夹中的 GUIDevGuide.pdf。 Chapter 7 is all about Menus.第 7 章是关于菜单的。

In ArrayedCollection i found "growToAtLeast:" that could increase the capacity of groupSizes.在 ArrayedCollection 中,我发现“growToAtLeast:”可以增加 groupSizes 的容量。

...groupSizes growToAtLeast: 2.
...groupSizes at: 1 put: 4.
...groupSizes at: 2 put: 1.
..etc..

I'm not sure this is the correct way, and I also dont know why I increased the size to 2, to get 4 index.我不确定这是正确的方法,我也不知道为什么我将大小增加到 2,以获得 4 个索引。 But it works.但它有效。 Pleace tell me if this is the wrong way.请告诉我这是否是错误的方法。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM