Is it possible to create a multidimensional array of gtkwidgets? Specifically something like this:
mywidgetlist[2]["title"];
Or should I be doing this in a different way? How would I do this?
Basically I have a number of "widgets" (Loaded from gtkbuilder) composed of smaller widgets and I want to be able to change certain values, so this array setup seems preferable.
Is there another way of doing this (Other than actually coding a complete widget using signals etc and placing them in a simple array?)
In C, you cannot use a string to index into an array. Or, strictly speaking you can, but it's almost never what you want to do.
For C solution using glib (handy if you already use GTK+), consider a single-dimensional array of GHashTable pointers.
Related
I want to work with a dynamic array using visual basic and I want that each element of the array will be an individual dynamic array too. How I should proceed? Thanks.
Read this - https://msdn.microsoft.com/en-us/library/aa716275(v=vs.60).aspx
And also this, in case you decide that reDim-ing things is annoying: https://msdn.microsoft.com/en-us/library/system.collections.arraylist(v=vs.110).aspx
Essentially though, you want to make an array and fill it with arrays. Then just treat those arrays independently for re-sizing purposes.
I want to compare two arrays of objects.
but there is not suitable method found for that method since it doesnt accept objects other from String, Integer etc..
I already override Equals method on the objects of the array.
But how do i pass the array to the method?
Assert.assertArrayEquals(esperado.getListaEquiposTorneo(), resultado.getListaEquiposTorneo());
//esperado.getListaEquiposTorneo(), resultado.getListaEquiposTorneo()) list 1 and 2 of objects made by me
First, you should be able to just use assertEquals
Assert.assertEquals(esperado.getListaEquiposTorneo(),
resultado.getListaEquiposTorneo());
I prefer to use Hamcrest as it gives better error messages
assertThat(actualArray,
IsArrayContainingInOrder.arrayContaining(
expectedArray));
assertThat(resultado.getListaEquiposTorneo(),
IsArrayContainingInOrder.arrayContaining(
esperado.getListaEquiposTorneo()));
IsArrayContainingInOrder
See Tomasz Nurkiewicz answer:
ArrayUtils.isEquals() from Apache Commons does exactly that. It also handles multi-dimensional arrays.
you can use a simple AssertTrue on the result
is there a way to create a fixed size array in LabView?
I know that I can do some check on the array size, then discard values when an array size become greater than a specific value. But, I think that is a common problem, so there is some built in function in LabView to have a fixed size array?
As far as I know this is impossible, unless they changed something in one of their latest releases but I doubt it: it would probably require a serious rewrite of the core array code.
The closest you can get is writing your own (possibly polymorphic) array class in which you encapsulate an actual array, that you initialize once with a certain size. For the rest your class only exposes methods to get/set by index. No resize etc.
Or, if you are talking about arrays of controls etc on the front panel, you can probably do this at the UI level by hide the indexing control from it and making sure it cannot be resized graphically. Or probably it's also doable to create a custom control and strip lots of array functionality from it.
If the array size is fixed at design time, then you might consider using a cluster instead. There is even a primitive to convert an array to a cluster of fixed size, provided the length is less then 257. (Array To Cluster function.)
There is also a primitive to go the other way if you need to index the array.
One implementation that you could do is a queue with a fixed size. You can use preview queue and flush queue to implement the functionality you want. However a specific custom class is probably a better idea.
In regular desktop LabVIEW, fixed-sized arrays would be something you'd have to code as per the answers you've already gotten here. However, in LabVIEW FPGA with, say, cRIO, all arrays must be fixed-size.
When calling the Call Library Function Node to a WINAPI DLL, there are times where a structure element may be officially be defined as BYTE[130]. So how do you absolutely, positively make sure your cluster has exactly the space for 130 bytes?
You can't do it with arrays no matter what, because LabVIEW arrays are pointers to a structure (the first element being the length), meaning any array you insert will only allocate enough space for a pointer, 4 bytes.
The work-around I came up with is to insert a cluster that includes sixteen U64 and one U16, pass that through an unflatten to string and you'll find it's exactly 130 bytes long.
When the cluster returns from the call, merely type cast the flattened into string results into a U8 array
I can see that it's possible to write functions like map/sortBy/findIndex and some other List-related functions for Arrays instead (at least those indexed by integers.) Is this done anywhere in the standard library, or would I need to roll my own?
I need to use an array in my program for the in-place update, but there are also several locations I'd like to use some of the above list functions on it. Is converting back and forth between the two the best solution?
(The arrays I've been looking at are from Data.Array.IArray. I'm also happy to use any other array library that implements this functionality.)
I recommend you have a look at the vector and vector-algorithms packages. They contain very efficient implementations of many common operations on Int-indexed arrays, in both mutable and immutable variants.
fmap (from Control.Monad) is sort of like a generic version of map that works on anything that supports the Functor type class. Array supports that, so you should be able to use fmap instead of map for array.
As hammar says, the vector and vector-algorithms are probably a better way to approach the problem if you need to consider indexed arrays.
Is there an elegant way to express
val a = Array.fill(2,10) {1}
def do_to_elt(i:Int,j:Int) {
if (a.isDefinedAt(i) && a(i).isDefinedAt(j)) f(a(i)(j))
}
in scala?
I recommend that you not use arrays of arrays for 2D arrays, for three main reasons. First, it allows inconsistency: not all columns (or rows, take your pick) need to be the same size. Second, it is inefficient--you have to follow two pointers instead of one. Third, very few library functions exist that work transparently and usefully on arrays of arrays as 2D arrays.
Given these things, you should either use a library that supports 2D arrays, like scalala, or you should write your own. If you do the latter, among other things, this problem magically goes away.
So in terms of elegance: no, there isn't a way. But beyond that, the path you're starting on contains lots of inelegance; you would probably do best to step off of it quickly.
You just need to check the array at index i with isDefinedAt if it exists:
def do_to_elt(i:Int, j:Int): Unit =
if (a.isDefinedAt(i) && a(i).isDefinedAt(j)) f(a(i)(j))
EDIT: Missed that part about the elegant solution as I focused on the error in the code before your edit.
Concerning elegance: no, per se there is no way to express it in a more elegant way. Some might tell you to use the pimp-my-library-Pattern to make it look more elegant but in fact it does not in this case.
If your only use case is to execute a function with an element of a multidimensional array when the indices are valid then this code does that and you should use it. You could generalize the method by changing the signature of to take the function to apply to the element and maybe a value if the indices are invalid like this:
def do_to_elt[A](i: Int, j: Int)(f: Int => A, g: => A = ()) =
if (a.isDefinedAt(i) && a(i).isDefinedAt(j)) f(a(i)(j)) else g
but I would not change anything beyond this. This also does not look more elegant but widens your use case.
(Also: If you are working with arrays you mostly do that for performance reasons and in that case it might even be better to not use isDefinedAt but perform validity checks based on the length of the arrays.)