I have no idea what this test is looking for - test-class

assertEquals(Modifier.STATIC | Modifier.PRIVATE, d.getClass().getDeclaredField("TYPES").getModifiers());
I have a fixed array that I wrote as a field like this:
private String[] TYPES;
and I initialized it like this:
TYPES= new String[3];
TYPES[0]="surveillance";
TYPES[1]="missile";
TYPES[2]="bombing";
Am i doing something wrong?

Related

Pass 2d array in get method

I have to pass a 2d integer array to get REST call.
Example:[[1,2,3], [5,10,11]]
I can pass it by entering data separately into the parameter like
Example: http://localhost:1136/demo?x=3,4,5&x=10,11,12
But I want it to be like
?x=[[1,2,3], [5,10,11]]
Any suggestion
You can define a #RequestParam(value= "x") String[][] like this:
#GetMapping(value = "/demo")
public void demo(#RequestParam(value= "x") String[][] array2d) {
// You will get: array2d = [[3,4,5],[10,11,12]]
}
Note: You may get the valid characters are defined in RFC 7230 and RFC 3986 error.
You can add to application.properties file something like this:
tomcat.relaxed-query-chars="|,{,},[,]"

Collect list of strings to map with Java 8 Stream API

Could someone suggest how could I transform list like ["bla", "blabla", "blablabla"] to map like {"bla" : 3, "blabla" : 6, "blablabla" : 9} with words stands for keys and values stands for words lengths?
I do something like:
Map<String, Integer> map = list.stream().collect(Collectors.groupingBy(Function.identity(), String::length));
but have no luck.
Thank you!
You were almost correct with groupingBy, but the second parameter of that is a Collector, not a Function. Thus I used toMap.
Map<String, Integer> map = Stream.of("bla", "blabla", "blablabla").distinct()
.collect(Collectors.toMap(Function.identity(), String::length));
You don't have to do something so complicated.
Here is an example code:
...
...
List<String> ll= // the list of words that you have.
Map<String,Integer> map = new HashMap<>();// or any other kind of map you want to create
for(String s:ll){
map.put(s,s.length());
}
Now you have a map that satisfies your requirement.

Sort a structure by one of its elements

I have this structure:
Structure Candidat
Public Nume As String
Public Scoala As String
Public Localitatea As String
Public Optiuni As String
Public Nota1 As Integer
Public Nota2 As Integer
End Structure
Public cd(100) As Candidat
I need to print the elements of the structure in a txt file, but ordered by name (by cd.Nume)
For i = 0 to some_number
PrintLine(1, cd(i).Nume, TAB(20), cd(i).Scoala, TAB(40), cd(i).Localitatea, TAB(60), cd(i).Optiuni, TAB(80), cd(i).Nota1, TAB(90), cd(i).Nota2)
Next
I saw something here but I don't understand.
Assuming you use vb.net as the programming language.
From the post you reference - the accepted answer is (by #Fredrik Mörk):
"
Assuming that the structure has a property called MPH:
cars = cars.OrderBy(Function(c) c.MPH)
Note: the above code was auto-converted from the following c# code (in case it contains errors):
cars = cars.OrderBy(c => c.MPH);
"
so you'll need to call
cd = cd.OrderBy(c => c.Nume); right before the printing for loop.

How can I convert a session variable to a string array?

I’m trying to store an array in a session variable then use it latter like this:
Session["sessionVariable"] = searchString;
Now here I’m trying to store the session variable into a string variable.
String[] sv = Session["sessionVariable"];
When I do I get his error.
Cannot explicitly convert type ‘Object’ to ‘String[]’, An explicit conversion exists, (are you mission a cast?)
I’ve tried various conversions but can’t find the correct one. Can you please help me to find the correct conversion? Thanks.
Try this:
Set:
Session["test"] = new string[] { "1", "2", "3" };
Get:
string[] array = Session["test"] as string[];

Run multiple string replaces with fewer calls to .replace()

I'd like to condence the following code into fewer calls to .replace(). It doesn't look like .replace() will do this. Am I right or am I just reading the documentation wrong?
public void setBody(String body) {
this.body = body.replace("“", "\"").replace("”", "\"").replace("—", "-").replace("’", "'").replace("‘", "'");
}
You should be able to use body.replace(['"', '—', '‘'], ['\"', '-', "'"]).
You are right. To solve this, you should create a StringBuilder and go through your string 1 character at a time, adding the character to the stringBuilder if it is correct or replacing if it is wrong.

Resources