How to use StringArray in java - java

I've been using Java to create a web service. It is a forum and I use a database for storing topics, comments and replies.
When retrieving the replies/comments from the server, I store them in an ArrayList<String[]> where each String[] holds the text and the ID of the comment/topic it is related to. However when I receive it on the client side I am forced to accept it as a List<StringArray> object. All other posts only refer to the normal String[]. Could someone please explain how to use a StringArray (not String[]).

By default there is no "StringArray" datatype in java.
Also java does not provide any class called "StringArray" similar to StringBuilder/StringBuffer by default.
So then what could be "StringArray" ? It is most definitely one of the below
User defined class created by somebody in your project.
A class provided by a third party library/toolset you project is using.
So you should first find out if it a user defined class or else what package is providing "StringArray" class and then try to understand how it works.
Going by the information you have provided "Map" seems like a better alternative on server/client side.
A Quick google search shows many third party libraries providing "StringArray" class.
http://grepcode.com/file/repo1.maven.org/maven2/org.jibx/jibx-bind/1.2.4.5/org/jibx/util/StringArray.java#StringArray.%3Cinit%3E%28java.lang.String%5B%5D%29
https://uima.apache.org/downloads/releaseDocs/2.3.0-incubating/docs/api/org/apache/uima/jcas/cas/StringArray.html
https://jna.java.net/javadoc/com/sun/jna/StringArray.html

A String array in Java is written as String[]. You make an instance of it like that:
String[] stringArray = new String[10]; //Entries 0 - 9
But in your case I would recommend to use an ArrayList or a Map (if you have to save the ID with it).

Related

How to convert ArrayList to ExampleSet in Rapidminer?

I'm creating an extension for rapidminer using java. I have an array of elements of type Example and I need to covert it to a dataset of type ExampleSet.
Rapidminer's ExampleSet definition looks like this:
public interface ExampleSet extends ResultObject, Cloneable, Iterable<Example>
I need to pick certain elements from dataset and send it back, still as ExampleSet, however casting is not working and I can't simply create new ExampleSet object since it's an interface.
private ExampleSet generateSet(ExampleSet dataset){
List<Example> list = new ArrayList<Example>();
// pick elements from sent dataset and add them to newly created list above
return (ExampleSet)list;
}
You will need more than a simple explicit cast.
In RapidMiner, an ExampleSet is not just a collection of Example. It contains more complex information and logic.
Therefore, you need another approach to work with ExampleSets. Like you already said, it is just the interface, which lead us to choice of the right subtype.
For starters, (Since: 7.3) simply use one of ExampleSets class's methods .
You also need to define each Attribute this ExampleSet is going to have, namely the columns.
Below, I create one with a single Attribute called First
Attribute attributeFirst = AttributeFactory.createAttribute("First", Ontology.POLYNOMINAL);
ExampleSetBuilder builder = ExampleSets.from(attributeFirst);
builder.addDataRow(example.getDataRow());
ExampleSet result = builder.build();
You can also get the Attributes in a more generic way using:
Attribute[] attributes = example.getAttributes().createRegularAttributeArray();
ExampleSetBuilder builder = ExampleSets.from(attributes);
...
If you have many cases where you have to create or alter ExampleSet, I encourage you to write your own ExampleSetBuilder since the original implementation have many drawbacks.
You can also try searching for other extensions, which may already meet your requirements, and you do not need to create one of your own (belive me, it's not Headache-free).
the ExampleSet class is getting deprecated (but still perfectly fine to use).
You might want to consider switching over to the newer data set API called Belt (https://github.com/rapidminer/belt). It's faster and more intuitive to use. It's still actively developed, so feedback is also welcome.
Also if you have more specific questions, feel free to drop by the RapidMiner community (https://community.rapidminer.com/), where also many of the developers are very active.

How to save multiple pieces of data as one package

I am trying to save two strings and a string array together in one package to an internal storage. But I am having an issue actually combining the three together as one. Here is my code:
class Save_Constructor extends Phone_Save{
String constructor_file_name;
String constructor_class_name;
String[] constructor_notes_to_attach;
Save_Constructor(){
constructor_file_name = file_name; constructor_class_name=name; constructor_notes_to_attach=text;
}
}
Ideally, I would like to compress these three elements into one, and return them back to the Phone_Save class to be saved into the Phones internal storage. Does anybody have an idea as to how to do this? Honestly I am not even sure whether or not to use a constructor for this, so I am open for any and all ideas. Thanks!
Just have a look at Java POJO/Bean class and try to understand it in detail. That is the solution for your problem. Just user setters and getters in your class and the use that Class's object as a single entity for your 3 different entities.
Just a quick google search and I got this for you.
Comment below if you dont understand anything.
Have a look at StringTokenizer Class. You have two ways to approach this. Either you put all the data into one String or into the StringArray. Iterate with for loop to put all this together and then call your store mothod.

2D Array that can hold multiple values with no limits

I am quite new to java currently working on a not-so-simple web browser application in which I would like to record a permanent history file with a 2D array setup with 3 columns containing "Date Viewed", "URL", "How many times this URL has been viewed before".
Currently I have a temporary solution that only saves "URL" which is also used for "Back, Foward" features using an ArrayList.
private List tempHistory = new ArrayList();
I am reading through the Java documentation but I cannot put together a solution, unless I am missing the obvious there is no 2D array as flexible a ArrayList like in Python?
From your description it doesn't sound like you need a 2D array. You just have one dimension -- but of complex data types, right?
So define a HistoryItem class or something with a Date property for date viewed, URL for URL, int for view count.
Then you just want a List<HistoryItem> history = new ArrayList<HistoryItem>().
The reason I don't think you really want a 2D array-like thing is that it could only hold one data type, and you clearly have several data types at work here, like a date and a count. But if you really want a table-like abstraction, try Guava's Table.
No, there is no built-in 2D array type in Java (unless you use primitive arrays).
You could just use a list of lists (List<List>) - however, I think it is almost always better to use a custom type that you put into the list. In your case, you'd create a class HistoryEntry (with fields for "Date viewed", URL etc.), and use List<HistoryEntry>. That way, you get all the benefits a proper type gives you (typechecking, completion in an IDE, ability to put methods into the class etc.).
How do you plan to browse the history then? If you want to search the history for each url later on then ArrayList approach might not be efficient.
I would rather prefer a Map with URL as key.
Map<Url,UrlHistory> browseHistory = new HahMap<Url,UrlHistory> ();
UrlHistory will contains all the fields you want to associate with a url like no. of times page was accessed and all.

How to create a variable name at runtime?

I am not able to create the name of the object at runtime. My statement is:
Map<String,String> objectName+""+lineNumber = new HashMap<String,String>();
It's giving me compiletime error. I want to create the HashMap object at runtime depending upon the line number.
Java is not a interpreted but rather a compiled language. So the compiler does not knows how to handle this. Such a thing might make sense in a scripting language.
If you need a custom Name for a "variable" maybe a construct like the following might make sense:
Map<String,Map<String,String>> varMap = new HashMap<String,Map<String,String>>();
varMap.put(objectName+" "+lineNumber, new HashMap<String, String>());
You can't do this directly in Java (without major tricks)
What you can (and probably should) do:
Put your Map in another map which has the 'variable' name as a key.
If you really want to do that you have to do code generation. For this again you have multiple options:
Generate Java Source Code and compile it
Generate Java Byte Code on the fly. You might wanna look at this list: http://java-source.net/open-source/bytecode-libraries for libraries available.
Having a dynamic object name is of No Use.
At first, it's not possible to give reference a dynamic name. The bigger question is Why do you want to do it?
If, just for learning and doing experiments, I'll suggest you should follow proper exercises.
But, if you are trying to achieve some project requirement, Pls. explain the requirement. There will be some other way to achieve that.

Mongo ReflectionDBObject, map ALL embedded array's elements to a class;

I use Mongo with native Java driver (no 3rd party library/ORM). I have this:
public class Release extends ReflectionDBObject {
//other fields omitted
private List<ReleaseDetailsByTerritory> releaseDetailsByTerritory = new ArrayList<ReleaseDetailsByTerritory>();
}
public class ReleaseDetailsByTerritory extends ReflectionDBObject { //...}
If I want to retrieve a "Release" entry having two "ReleaseDetailsByTerritory" entries, and have them auto-instanciated in a Release class instance containing a List of two ReleaseDetailsByTerritory class instances, I have to do this:
releaseColl.setObjectClass(Release.class);
releaseColl.setInternalClass("ReleaseDetailsByTerritory.0", ReleaseDetailsByTerritory.class);
releaseColl.setInternalClass("ReleaseDetailsByTerritory.1", ReleaseDetailsByTerritory.class);
Release r = (Release) releaseColl.findOne();
i.e. I need to specifically map each potential element of the embedded array to the corresponding class.
Is there a way to tell the Mongo driver that I want all and any element of an embedded array to be mapped to a certain class? Something like :
collection.setInternalClass("ReleaseDetailsByTerritory.*", ReleaseDetailsByTerritory.class);
?
Thanks. And please don't say "use Spring MondoDb module or Morphia". I want to know if this is achievable with the Mongo native Java driver.
Looking at the source code and I don't think this is possible. There is also no obvious way to create convenience functionality for what you need. Having to call setInternalClass for each array element is hardly an option given the massive amounts of memory usage this would result in for large arrays.
You may want to consider implementing your own implementation of a "Document" class that does what you need without having to go to a full mapping solution such as Morphia (which is actually pretty elegant, more so than Spring at least).
You could also consider opening a JIRA issue at jira.mongodb.org and request this feature.

Categories

Resources