Why doesn't the code work?
List<String> list = new ArrayList<>();
//or
List list = new ArrayList<String>();
//or
List<String> list = new ArrayList<String>();
//or
List list = new ArrayList();
How i understand, list is interface. ArrayList implemented list. Why doesn't type conversion work?
Related
I am building a bot that monitors HN for topics that I am interested in.
I'd like to analyze an in-memory string, and determine if it contains some keywords that I am interested in.
I'd like it to take into consideration the things that Lucene does when performing a standard query (word stemming, stop words, normalizing punctuation, etc).
I could probably build an in-memory index, and query it using the normal approach, but is there a way that I can use the internals of Lucene to avoid a needless index being built?
Bonus points if I can get a relevance value (0.0-1.0), instead of just a true/false value.
Pseudo code:
public static decimal IsRelevant(string keywords, string input)
{
// Does the "input" variable look like it contains "keywords"?
}
IsRelevant("books", "I just bought a book, and I like it."); // matching!
IsRelevant("book", "I just bought many books!"); // matching!
I created a solution using an in-memory search index. It's not ideal, but it does the task.
public static float RelevanceScore(string keyword, string input)
{
var directory = new RAMDirectory();
var analyzer = new EnglishAnalyzer(LuceneVersion.LUCENE_48);
using (var writer = new IndexWriter(directory, new IndexWriterConfig(LuceneVersion.LUCENE_48, analyzer)))
{
var doc = new Document();
doc.Add(new Field("input", input, Field.Store.YES, Field.Index.ANALYZED));
writer.AddDocument(doc);
writer.Commit();
}
using (var reader = IndexReader.Open(directory))
{
var searcher = new IndexSearcher(reader);
var parser = new QueryParser(LuceneVersion.LUCENE_48, "input", analyzer);
var query = parser.Parse(keyword);
var result = searcher.Search(query, null, 10);
if (result.ScoreDocs.Length == 0)
{
return 0;
}
var doc = result.ScoreDocs.Single();
return doc.Score;
}
}
What is this ??
ArrayList [] set = new ArrayList[n];
Implementation of arraylist is:
ArrayList Integer> al = new ArrayList Integer> ();
The first what you had defined
ArrayList [] set = new ArrayList[n];
This is an array of ArrayList. As we know that ArrayList is a class. So the first statement defines the array of ArrayList class.
The second example is
ArrayList Integer> al = new ArrayList Integer> ();
Here you are creating the object of ArrayList. So here you are creating an object of ArrayList class and will be adding values in it.
Below is an example that contains the use of both
public class Testing {
public static void main(String[] args) {
ArrayList[] set = new ArrayList[10];
ArrayList<String> list = new ArrayList<String>();
list.add("RHV");
list.add("Zen");
set[0] = list;
System.out.println(set[0]);
}
}
I want to test a data in html table.and verify it with database which contains the staff information. Below is the code. now i am stuck for creating assert, how to collect the rows or list of list of the table values
#Test
public void TestingReport()
{
driver.findElement(By.xpath(".//*[#id='content']/h2[2]/a")).click(); // click on admin
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
driver.findElement(By.xpath("//*[#id='content']/h2[2]/a")).click(); // click on staff member
driver.manage().timeouts().implicitlyWait(10, TimeUnit.SECONDS);
// ArrayList<ArrayList> ActualReport= new ArrayList<ArrayList>();
List<WebElement> tablerow = driver.findElements(By.xpath("//table//tr"));
ArrayList <String> cellsStr = new ArrayList<>();
ArrayList<List> table = new ArrayList<>();
List<WebElement> cells= null ;
// System.out.println(tablerow.size());
for(int i=1;i<tablerow.size();i++) {
cells = driver.findElements(By.xpath("//tr["+i+"]//td"));
// table.add(cells);
for(int j=4;j<cells.size();j++)
{
System.out.println(cells.get(j).getText());
cellsStr.add(cells.get(j).getText());
}
System.out.println(cellsStr);
table.add(cellsStr);
System.out.println(table);
System.out.println(cellsStr);
}
System.out.println(table);
}
Here i am trying to get an Arraylist of sting in to a string, but before the next iteration i am clearing the Arraylist of string, but it is making the List of array list also empty.
Below helps to get the data from list of list of values.
List<WebElement> tablerow = driver.findElements(By.xpath("//table//tr"));
for(int i=1;i<tablerow.size();i++)
{
List<WebElement> cells= tablerow.get(i).findElements(By.tagName("td"));
for(int j=4;j<cells.size();j++){
List<WebElement> cellSubRow= cells.get(i).findElements(By.tagName("td"));
System.out.println(cellSubRow.get(j).getText());}}
I want to generate below code snippet using Velocity.
Map<String, List<String>> fruitsAndTypesMap = new HashMap<String, List<String>>();
String fruit_Apple = "Apple";
List<String> types_Apple = new ArrayList<String>();
types_Apple.add("KA");
types_Apple.add("WA");
fruitsAndTypesMap.put(fruit_Apple, types_Apple);
String fruit_Orange = "Orange";
List<String> types_Orange = new ArrayList<String>();
types_Orange.add("HO");
types_Orange.add("LO");
fruitsAndTypesMap.put(fruit_Orange, types_Orange);
My template_fruits.vm file is as below.
#if ($fruitsMap.size()>0)
Map<String, List<String>> fruitsAndTypesMap = new HashMap<String,List<String>>();
#foreach( $fruitName in $fruitsMap.keySet() )
String fruit_$fruitName = "$fruitName";
List<String> types_$fruitName = new ArrayList<String>();
#foreach( $fruitType in $fruitsMap.get($fruitName) )
types_$fruitName.add("$fruitType.name");
#end
fruitsAndTypesMap.put(fruit_$fruitName, types_$fruitName);
#end
#end
Issue is with the statement: types_$fruitName.add("$fruitType.name");
it does not evaluate the $fruitName properly.
But if i modify the statement to be like: types_($fruitName).add("$fruitType.name");, it is evaluated properly but the value is surrounded with brackets. I dont understand the brackets trick for evaluation.
Below code can be used to load $fruitsMap
public static Map<String, List<Fruit>> getFruitsMap(){
Map<String, List<Fruit>> fruitsMap = new HashMap<String, List<Fruit>>();
List<Fruit> applesList = new ArrayList<Fruit>();
Fruit fruit_Apple = null;
fruit_Apple = new Fruit(); fruit_Apple.setName("KA");
applesList.add(fruit_Apple);
fruit_Apple = new Fruit(); fruit_Apple.setName("WA");
applesList.add(fruit_Apple);
List<Fruit> orangesList = new ArrayList<Fruit>();
Fruit fuit_Orange = null;
fuit_Orange = new Fruit(); fuit_Orange.setName("HO");
orangesList.add(fuit_Orange);
fuit_Orange = new Fruit(); fuit_Orange.setName("LO");
orangesList.add(fuit_Orange);
fruitsMap.put("Apple", applesList);
fruitsMap.put("Orange", orangesList);
return fruitsMap;
}
Code related to template execution:
VelocityEngine velEngine = new VelocityEngine();
velEngine.init();
Template template = velEngine.getTemplate("template_fruits.vm");
VelocityContext context = new VelocityContext();
context.put("fruitsMap", FruitClient.getFruitsMap());
StringWriter writer = new StringWriter();
template.merge(context, writer);
System.out.println("Content: " + writer.toString());
Any hints of what is the wrong with the statement--> types_$fruitName.add("$fruitType.name"); will be helpful.
Thanks
The statement should be:
types_${fruitName}.add("$fruitType.name");
Otherwise, Velocity tries to call an add method on the $fruitName object.
I've tried so many different ways, but I can't get the check box to be checked! Here's what I've tried:
var reader = new iTextSharp.text.pdf.PdfReader(originalFormLocation);
using (var stamper = new iTextSharp.text.pdf.PdfStamper(reader,ms)) {
var formFields = stamper.AcroFields;
formFields.SetField("IsNo", "1");
formFields.SetField("IsNo", "true");
formFields.SetField("IsNo", "On");
}
None of them work. Any ideas?
You shouldn't "guess" for the possible values. You need to use a value that is stored in the PDF. Try the CheckBoxValues example to find these possible values:
public String getCheckboxValue(String src, String name) throws IOException {
PdfReader reader = new PdfReader(SRC);
AcroFields fields = reader.getAcroFields();
// CP_1 is the name of a check box field
String[] values = fields.getAppearanceStates("IsNo");
StringBuffer sb = new StringBuffer();
for (String value : values) {
sb.append(value);
sb.append('\n');
}
return sb.toString();
}
Or take a look at the PDF using RUPS. Go to the widget annotation and look for the normal (/N) appearance (AP) states. In my example they are /Off and /Yes: