How compare two arrays of objects with assertArrayEquals - arrays

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

Related

How does Ruby's Combined Comparison Operator work?

First question on stackoverflow :)
I'm going through the Ruby course on Codecademy and I'm stuck on something.
fruits = ["orange", "apple", "banana", "pear", "grapes"]
fruits.sort! {|first, second| second <=> first}
print fruits
I don't know how to phrase this question. On Codecademy the assignment was to set up the array to be displayed in reverse on the console. After some research, I was able to figure it out. I understand how it works and the order to put it in the code not why. I'm aware that "<=>" compares two objects, but how do the items within the array become objects when we don't declare them as such?
Secondly, what is the purpose of writing this code in this way when we could do fruits.sort.reverse?
First question: At various points in its operation the sort method has to compare pairs of objects to see what their relative ordering should be. It does the comparison by applying the block you pass to sort, i.e., {|first, second| second <=> first}. Not sure what you mean by "how do the items within the array become objects when we don't declare them as such?". All data in ruby is an object, so there's no declaration or conversion needed given that all variables are object references.
Second question: Yes, you could do fruits.sort.reverse, but that would require additional work after the sort to do the reverse operation. Also, reverse can't handle more complex sorting tasks, such as sorting people by multiple criteria such as gender & last name, or hair color, height, and weight. Writing your own comparator can handle quite complex orderings.
String literals can be used to create string objects in Ruby, there is no need to use the String class to create the object. The following two are equivalent:
"Hello, world!"
String.new("Hello, world!")
More information can be found here.
Secondly, what is the purpose of writing this code in this way when we could do fruits.sort.reverse?
Please contact Codecademy about this, but I suspect it's for learning more about how <=> works.

Create generic array from type specific arrays in Clojure

I am working with JFreeChart in clojure and came across a situation I'd like help.
JFreeChart's DefaultXYDataset has a method addSeries which is used to add series to the chart. The data is supposed to be an array containing two arrays which are type specific(Array of doubles). I therefore wrote the following code thinking it would work but i get a
ClassCastException that class [Ljava.lang.Object; cannot be cast to class [[D ([Ljava.lang.Object; and [[D are in module java.base of loader 'bootstrap').
(doto _dataset
(.addSeries "S1" (to-array (conj
[]
(double-array (range 10))
(double-array (range 10))))))
After looking through i realized that to-array converts the two nested arrays to #object["[Ljava.lang.Object;" 0x491223e7 "[Ljava.lang.Object;#491223e7"] instead of the intended #object["[D" 0x4f5cf37 "[D#4f5cf37"] #object["[D" 0x6d895193 "[D#6d895193"]. Is there a way of combining them to a generic array without converting them to arrays of longs? Maybe another method apart from to-array. Any other suggestions are welcome. Thanks.
Edit: #bfabry answer will work, i could use make-array and then use aset-double but this will result in me looping through the two sequences and assign their values to the main array. I am trying to avoid this as the two datasets can be quite big, even up to 300k items each.
That's two dimensional array, not an array of two array objects. You'll need to use make-array and aset-double to make the array you want.
user=> (class (make-array Double/TYPE 2 2))
[[D
https://clojuredocs.org/clojure.core/make-array

Talend - How to pass an Array from a ChildJob to a ParentJob

How can I pass an array from a ChildJob to a ParentJob?
The array values came from : tExtractXMLField -> tFlowToIterate.
I've done it by using a Routine (like this: https://help.talend.com/pages/viewpage.action?pageId=5671107), but I'm interesting in another approach, where Routines are no needed.
This could be very very tricky, since the only way to pass variables to a subjob is through context vars, which are Strings.
You can concat the elements of the array (in a tJavaRow, for example) and then feed a String context var to pass and split() and cast inside the child job. It's a quick-and-dirty solution, but only suitable for small arrays, obviously.
I'm afraid the official KB you linked is the most advisable way to do it, even if you need a user routine

Haskell map/sortBy/findIndex etc. for Arrays instead of Lists

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.

Multidimensional array of gtkwidgets

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.

Resources