multi dimensional string array in C# - string

I need to create a multi dimensional array of strings in C# like-
tiger elephant
pigeon lion
ant peacock
I'm unable to create. It either flags an error or throws an exception at the run time.
Please help me with it, by giving the correct syntax.

string[] animals = new string[]{"tiger", "elephant", "pigeon", "lion", "ant", "peacock"};

Am not sure I understand the question, the above doesn't appear to be a multidimensional array.
Do you simply want somthing along the lines of the below?
string[] animals = {"tiger", "elephant"};

LOOK AT THIS
string[,] siblings = new string[2, 2] { {"Mike","Amy"}, {"Mary","Albert"} };
and also look for jagged array

Here is an example of how to create multi-dimensional string array and have it initialized:
string[] myStringArray = new string[] { tiger, elephant, pigeon, lion, ant, peacock};

Related

(Groovy) Add multiple values to an ArrayList

like the title says, I want to add multiple values to my arrayList in Groovy.
But it is not accepted.
Collection<String> actorCollection = new ArrayList<>();
actorCollection.add("first","second");
If there is only a single value, it works perfectly.
Thanks in advance!
Use addAll: actorCollection.addAll("first","second")
Note: Groovy's list literal will give you an array list. So could just write def actorCollection = [] or even ... = ["first", "second"] to fill the list with the values right from the beginning.

Spark IntArrayParm in JAVA

I am trying to test a number of multilayer perception network architectures. So, I am training a model via a crossvalidation using different params. However, I fail to set up layer param using JAVA. Not sure how this is done, but none of the following work:
int[] layers1 = new int[]{10,1,3,2};
IntArrayParam p = new IntArrayParam(null, "name", "doc");
p.w(layers1);
int[] layers2 = new int[]{10,1,3,2};
IntArrayParam p2 = new IntArrayParam(null, "name", "doc");
p2.w(layers2);
builder.addGrid(mlpc.layers(), JavaConverters.asScalaIterableConverter(Arrays.asList(p,1)).asScala());
Sending a list of arrays (or a multidimensional array):
builder.addGrid(mlpc.layers(), JavaConverters.asScalaIterableConverter(Arrays.asList(1,2,2), Arrays.asList(1,2,2)).asScala());
I am not sure how this i suppose to be done in JAVA, and was not able to find any examples. Any ideas appreciated.
Best,
Ilija
After some research got it, just in case anyone gets stuck using IntArrayParam on Java here is an example:
//build network parameters grid
int[] layers1=new int[]{17,8,4,26};
int[] layers2=new int[]{17,12,8,26};
//use scala collections converters to get a Scala Iterable of Int[]
scala.collection.Iterable<int[]> iter= JavaConverters.iterableAsScalaIterableConverter(Arrays.asList(layers1, layers2)).asScala();
Hope that helps!

How can I retrieve the geometry coordinates of a NpgsqlTypes.PostgisGeometry type field from the NpgsqlDataReader?

.NET 4.5, C#, Npgsql 3.1.0
I have a query which retrieves a Postgis geometry field - the only way I could see of doing this was:
public class pgRasterChart
{
...
public NpgsqlTypes.PostgisGeometry GEOMETRY;
...
}
...
NpgsqlDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
pgRasterChart chart = new pgRasterChart();
chart.GEOMETRY = (PostgisGeometry) reader.GetValue(21);
...
This functions but I need to get at the coordinates of the GEOMETRY field and I can't find a way of doing that? I want to use the coordinates to display the results on an OpenLayers map.
Any answers most gratefully received. This is my first post so my apologies if the etiquette is clumsy or question unclear.
Providing another answer because the the link above to the documentation for PostGisTypes is now broken.
PostGisGeometry is an abstract base class that does not contain anything more exiting than the SRID. Instead, you want to cast the object obtained by your datareader to the appropriate type (any of the following):
PostGisLineString
PostGisMultiLineString
PostGisMultiPoint
PostGisMultiPolygon
PostGisPoint
PostGisPolygon
These classes have ways of getting to the coordinates.
eg:
...
NpgsqlDataReader reader = command.ExecuteReader();
try
{
while (reader.Read())
{
var geom = (PostgisLineString) reader.GetValue(0);
var firstCoordinate = geom[0]; // Coordinate in linestring at index 0
var X = firstCoordinate.X;
var Y = firstCoordinate.Y;
...
As you can see here
https://github.com/npgsql/npgsql/blob/dev/src/Npgsql.LegacyPostgis/PostgisTypes.cs
PostgisGeometry types are a set of xy pairs.
For example, a linestring is an array of points, a polygon is an array of rings and so on..
You could traverse those structures and get the coordinates.
However, if you just want to display geometries using openlayers, I suggest you to use the wkt format.
You should change your query, selecting st_astext(geometry) instead of geometry, than treat the result as a string and give it back to OpenLayers.
Then use OpenLayers.Geometry.fromWKT to parse the WKT into an OpenLayers.Geometry

Un-nesting nested tuples to single terms

I have written an udf (extends EvalFunc<Tuple>) which has as output tuples with inner tuples (nested).
For example the dump looks like:
(((photo,photos,photo)))
(((wedg,wedge),(audusd,audusd)))
(((quantum,quantum),(mind,mind)))
(((cassi,cassie),(cancion,canciones)))
(((calda,caldas),(nova,novas),(rodada,rodada)))
(((fingerprint,fingerprint),(craft,craft),(easter,easter)))
Now I want to process each of this terms, distinct it and give it an id (RANK). To do this, i need to get rid of the brackets. A simple FLATTENdoes not help in this case.
The final output should be like:
1 photo
2 photos
3 wedg
4 wedge
5 audusd
6 quantum
7 mind
....
My code (not the udf part and not the raw parsing):
tags = FOREACH raw GENERATE FLATTEN(tags) AS tag;
tags_distinct = DISTINCT tags;
tags_sorted = RANK tags_distinct BY tag;
DUMP tags_sorted;
I think your UDF is return is not optimal for your workflow. Instead of returning a tuple with variable number of fields (which are tuples), it would be a lot more convenient to return a bag of tuples.
Instead of
(((wedg,wedge),(audusd,audusd)))
you will have
({(wedg,wedge),(audusd,audusd)})
and you will be able to FLATTEN that bag to:
1. make the DISTINCT
2. RANK the tags
To do so, update your UDF like this :
class MyUDF extends EvalFunc <DataBag> {
#Override
public DataBag exec(Tuple input) throws IOException {
// create DataBag
}
}

Subsonic load objects by a list of ids

Is it possible to load objects by a list of ids using subsonic ActiveRecord?
My code looks like:
IList<Video> videos = Video.Find(v => videoIds.Contains(v.ID));
I get an exception: The method 'Contains' is not supported
Do I do something wrong ... or I hit one of subsonic's limitations?
Thanks, Radu
After more research I found a way to achieve this:
List<int> videoIds = new List<int>(){1, 2, 3, 4, 5};
SqlQuery query = new Select().From<Video>().Where("ID").In(videoIds);
List<Video> videos = query.ExecuteTypedList<Video>();
FYI: SubSonic's linq parser does not like generic Lists and Contains
// does not work
List<int> videoIds = new List<int>() {1,2,3,4,5};
var videos = Video.Find(v => videoIds.Contains(v.ID));
// should work
IEnumerable<int> videoIds = new List<int>() {1,2,3,4,5};
var videos = Video.Find(v => videoIds.Contains(v.ID));
Noticed the difference?
Sounds strange, but whenever you want to use Contains() with Subsonic, you first have to cast your List to an IEnumerable to prevent the NotSupportedException.
This is the one-liner I believe you where looking for.
var colMatchingVideos = Video.Find( objVideo => colVideoIds.Any( iVideoId => objVideo.ID == iVideoId).ToList();
Also I would strongly advise you avoid using string literals for columns, instead you could use Video.Columns.Id or expressions Where( o => o.Id). This would ensure that if you change your column names in the Database a compile time exception will occur. Help's a lot with maintainability.

Resources