Anylogic - Using String Value to get an Object - java

I have a text object (Shape text) in the Main tab. Right now I am referring the text value by hard coding the name in the areas wherever I use.
I want to add more text objects like text1, text2 etc. which properly follows a pattern for the suffix numbers. I have a variable for count of text objects.
In a function, I want to go through all these text object values using a loop. I am able to generate Strings of "text1", "text2", "text3", etc. using simple for loop.
I want to know how to refer these objects and get the values in the text objects.
Thanks in Advance.

what you can instead, is create a collection (arraylist) that contains all these text objects, if they are ordered in the list the way you want, you can use the index of that collection as your identifier..
collection.get(index);
If you insist in using the name, you can make a collection of type LinkedHashMap where the key element class is String and the value elements class is ShapeText
Then you can know what the text is by using
collection.get("text1"); for instance

Related

How to store over 300 words with definitions in a Android studio

I'm trying to create an android app where you can learn hard words its over 300 words. I'm wondering how I should store the data in java.
I have a text file where all the words are. I have split the text so I have one array with the words and another Array with the definitions, they have the same index. In an activity, I want to make it as clean as possible, because sometimes I need to delete an index and It's not efficient to that with an ArrayList since they all need to move down.
PS. I really don't wanna use a database like Firebase.
Instead of using two different arrays and trying to ensure that their order/indices are matched, you should consider defining your own class.
class Word {
String wordName;
String wordDefinition;
}
You can then make a collection of this using ArrayList or similar.
ArrayList<Word> wordList;
I know you were concerned about using an ArrayList due to the large number of words, however I think for your use case the ArrayList is fine. Using a database is probably overkill, unless if you want to put in the whole dictionary ;)
In any case, it is better to define your own class and use this as a "wildcard" to collection types which accept these. This link may give you some ideas of other feasible data types.
https://developer.android.com/reference/java/util/Collections
I personally would use a HashMap.
The reason for this is because you can set the key to be the word and the value to be the definition of the word. And then you can grab the definition of the word by doing something like
// Returns the definition of word or null if the word isn't in the hashmap
hashMapOfWords.getOrDefault(word, null);
Check out this link for more details on a HashMap
https://developer.android.com/reference/java/util/HashMap

How to store global Array of multiple items from a list for later use

I have my selenium code which pulls a list of names available in the list.
Now I want to store the same names as global for later use. Please help.
I have tried array process, it pulls only true with each line, but not the values.
List<WebElement> allText = driver.findElements(By.xpath("//*[#id='pnlLeftMenu']/table/tbody/tr/td[2]/table[2]/tbody/tr[3]/td/table/tbody/tr"));
int total = allText.size();
System.out.println(total);
for(int i=3;i<=total;i++)
{
CaselevelSigningCMs =driver.findElement(By.xpath("//*[#id='pnlLeftMenu']/table/tbody/tr/td[2]/table[2]/tbody/tr[3]/td/table/tbody/tr"+"["+i+"]"+"/td[2]")).getText();
System.out.println(CaselevelSigningCMs);
}
I should get the names like: Ranjit Nyk, Sudhanva G.... I have to verify those names in other pages in other class/method. CaselevelSigningCMs is a global variable, it pulls single item only. I need similar array defined as global so that it can pull multiple items.
CaselevelSigningCMs =driver.findElement(By.xpath("//*[#id='pnlLeftMenu']/table/tbody/tr/td[2]/table[2]/tbody/tr[3]/td/table/tbody/tr"+"["+i+"]"+"/td[2]")).getText();
See how this String CaselevelSigningCMs is defined. Just like that go for ArrayList
Lets say the string used is defined some where like
public String CaselevelSigningCMs = "";
Similarly define ArrayList
public List<String> collectedItems=new ArrayList<>();
in loop add get text of each element, something like
collectedItems.add(driver.findElement(By.xpath("//*[#id='pnlLeftMenu']/table/tbody/tr/td[2]/table[2]/tbody/tr[3]/td/table/tbody/tr"+"["+i+"]"+"/td[2]")).getText());
You can use Arraylist to store and retrieve them
ArrayList ts=new ArrayList();
store the objects in arraylist
ts.add();
You can use Stream.map() function in order to convert the list of WebElements to the List of Strings containing element text attribute in a single shot:
List<String> allText = driver.findElements(By.xpath("//*[#id='pnlLeftMenu']/table/tbody/tr/td[2]/table[2]/tbody/tr[3]/td/table/tbody/tr"))
.stream()
.map(WebElement::getText)
.collect(Collectors.toList());
allText.forEach(System.out::println);
I also don't really like your XPath expression, especially:
//* wildcard
these table[2] and tr[3]
wildcard expressions take longer time to execute and consume more resources, and your approach seems to be very dependent on DOM structure so I would recommend using XPath Axes as well as XPath Functions / Operators to make your expression as relative, robust and reliable as possible.
You can also consider using Table class of the HtmlElements framework for working with HTML tables

How to separate each word in a file and display each word and number of each word in the table?

I don't know anything about that. May you explain with detail to me, please?
Load the contents of the file into a string
Call the split operation string.split(" ") and assign the result to an array object
Create a HashMap to store your results
Use a for loop to iterate over the array
If the value is already in the map map.contains("example") then update the value to increment the occurrence
Otherwise add the new value to the map map.put("example", 1)
There are many tutorials dealing with the steps outlined above and you should be able to track them down rather easily.

(Java) Saving/Loading an ArrayList using Properties

I have been researching different methods for saving and loading configuration settings for my application. I've looked into Preferences, JSON, Properties and XML but I think I've settled on using the Properties method for most of my application settings.
However, I'm not able to find any information on how to best save and load an ArrayList from that file. It seems there are only individual key/pair string combinations possible.
So my question is basically, is there a better way to do this? I have an ArrayList of Strings in my application that I need to be able to save and load. Can this be done with Properties or do I need to use a separate file just to hold this list and then read it in as an ArrayList (per line, perhaps)?
EDIT: I should mention, I would like to keep all config files as readable text so I am avoiding using Serialization.
You can use commas to place multiple values on the same key.
key:value1,value2,value3
Then split them using the split function of a string after reading them in which will give you a String[] array which can be turned into an ArrayList via Arrays.asList().
Here's a partial MCVE:
ArrayList<String> al = new ArrayList<>();
al.add("value1");
al.add("value2");
al.add("value3");
String values = al.toString();
//Substring used to get rid of "[" and "]"
prop.setProperty("name",values.substring(1,values.length() - 1);
I found that using the following combination worked perfectly in my case.
Save:
String csv = String.join(",", arrayList());
props.setProperty("list", csv);
This will create a String containing each element of the ArrayList, separated with a comma.
Load:
arrayList = Arrays.asList(csv.split(","));
Takes the csv String and splits it at each comma, adding the elements to the arrayList reference.
I've seen two approaches for writing lists to a Properties file. One is to store each element of the list as a separate entry by adding the index to the name of the property—something like "mylist.1", "mylist.2". The other is to make a single value of the elements, separated by a delimiter.
The advantage of the first method is that you can handle any value without worrying about what to do if the value contains the delimiter. The advantage of the second is that you can retrieve the whole list without iterating over all entries in the Properties.
In either case, you probably want to write a wrapper (or find a library) around the Properties object that adds methods to store and retrieve lists using whichever scheme you choose. Often these wrappers have methods to validate and convert other common data types, like numbers and URLs.

Two-dimensional array of different types

I want to create a two-dimensional array in which I want to store records from the database. So lets say that the first is of type int and the second of type String (here I am describing just one record so basically types of db columns). How can I do it? Is an array the right data structure for that?
I am not sure I am following, but you might be looking for a Map<Integer,String>. or Map<Integer,List<String>>. [have a look on List, and HashMap]
Map allows association of the key [Integer] to the value [String or List].
Map also allows fast lookup of key, and its attached value.
(*) You should use Map<Integer,List<String>> if you want to attach more then one String per Integer, or alternatively you can use apache commons MultiMap
Arrays can only contain one type. If that type happens to be Object then it can store Object and any of its sub-types, but that doesn't really sound like what you're trying to accomplish here.
It sounds like what you're describing is a 2D array to store database information, with each element in the array being a column in one of the rows. This isn't an array of records, it's an array of column data.
Instead, just store a one-dimensional array of records, where each element of the array is a reference to the entire DB row.
You can do the same thing with the help of this
Object[][] o = new Object[10][10];
o[0][0] = 1;
o[0][1] ="hello";
System.out.println(o[0][0]);
System.out.println(o[0][1]);
You can use
HashMap<Integer, ArrayList<String>>
If you simply want to have one column of String data and another column of int data, this is what you can consider doing:
Declare a 2 dimensional String array
String[][] words = new String[][];
Your first column can contain all the String data. The second column can have the numeric data but in the form of a String. You may want to use the Integer.toString() and Integer.parseInt() methods to do this
words[index][index] = Integer.toString(Integer.parseInt(args));
I'm not sure what exactly you hope to achieve but you may consider modifying this snippet to suit your needs

Categories