I am new to Java and I would like to create a Weibull distributed random value.
I tried using the the WeibullGen class from the umontreal.iro.lecuyer.randvar package but got kind of stuck.
I tried something like the following but it obviously doesn't work.
for(int i=0;i<5;i++){
int result = WeibullGen.nextDouble(RandomStream s ,1.0,1.0,1.0);
if(result>0) System.out.println(result);
}
My problem is that I don't know how to create a stream. I'm pretty sure that it can't be that hard, but I'm really struggling to find my way.
It is not a good idea to create a new stream for each generated random number. It is better to use only 1 stream like this:
RandomStream stream = new MRG32k3a();
for(int i=0;i<5;i++) {
int result = WeibullGen.nextDouble(stream, alp, lam,1.0);
System.out.println(result);
}
To create a Stream inline, you would do this:
for(int i=0;i<5;i++){
int result = WeibullGen.nextDouble(new RandomStream(),1.0,1.0,1.0);
if(result>0) System.out.println(result);
}
In Java, you have to instantiate objects with the new keyword.
Related
I'm trying to get my ArrayList's index via indexOf. So far, I've got
My ArrayList: public static ArrayList<Shop> allShops = new ArrayList();
That what is supposed to get the index
Scanner editShop = new Scanner(System.in);
String shopToEdit = editShop.nextLine();
int i = allShops.indexOf(shopToEdit);
System.out.println(i); //see what our index is (returns -1 because the var doesn't seem to get the right input)
EditFunc.edit(i);
and this, that is supposed to change my arraylist
public static void edit(int index){
//change array with given input in edit
//TODO: Make it so they can choose what to edit
//with booleans if editTrueName = false and then later on make it true again
System.out.println("Enter the new shop name:");
Scanner editedShopAttribute = new Scanner(System.in);
String editedShopName = editedShopAttribute.nextLine();
System.out.println("Enter the new shop location:");
String editedShopLocation = editedShopAttribute.nextLine();
Shop EditedVar = new Shop();
EditedVar.createShop(editedShopName,editedShopLocation);
allShops.set(index, EditedVar);
}
I've copied the values that debugger showed me and replaced them with that, but it still doesn't seem to work. Am I taking in the wrong kind of data? What can I try?
If there's something that looks wrong with my code, I'm always up to try and make it better.
Can't you do it with a Map<String, Shop>? That way you could use the shopName as a key.
By the way, as I see your new with java and OOP, I strongly recommend you read Clean Code, by Robert C. Martin, its a game-changing book.
I don't believe you can make what you want work with an Array. The reason as pointed out in one of the comments is that you are looking for a String, but the Array contains Shop(s). Since a Shop contains more than just the ShopName, you will never be able to find it this way. You should use a "Map" for such purposes:
public static Map<String, Shop> allShopsMap = new HashMap<>();
If you add all the shops to this map, then when you get a ShopName as an input, you merely need to do:
Shop shopToEdit = allShopsMap.get(inputShopName);
then call the set methods on this object to alter name and location.
Im trying to return an arraylist from the method getNumbers (which contains strings)
public ArrayList<String> getNumbers(){
return (numeros);
}
Then by using a searcher im trying to compare between a variable m (which contains the desired info to look for) and the returned list.
public class NumberSearcher {
Reader reader = new KeyboardReader();
public NumberSearcher(ArrayList<Contacto> contactos){
String m = reader.read();
for(int i = 0; i<contactos.size();i++){
if(contactos.get(i).getPhoneNumbers().contains(m)){
contactos.get(i).display();
}
}
}
}
I have succeded in creating a searcher using this very same style but only when using methods that return String alone.
The problem is its not working. If there there would be a match it should display the contact information but it seem it isnt "comparing" properly because nothing happens.
It's difficult to understand what you're asking here. Your getNumbers method doesn't get called from the second code block, so I don't see where that is relating to anything. It's also unclear what you mean the problem is. Can you try to give us a more detailed description of what is going wrong?
Anyways, I'll try to give you some general advice here, but without knowing the issue it's hard to say how much this will help.
Firstly, it is almost always recommended to have your method's return type as the List interface, rather than a specific implementation (ArrayList, etc). You can specify a return type from within the method but this way they client doesn't need to know what the underlying data structure is, and you are also flexible to future data structure changes.
public List<String> getNumbers(){
return (numeros);
}
Secondly, I would probably change the name 'getNumbers' to something slightly more precise - if I see a 'getNumbers' method I expect it to return some numeric entities, not a list of strings. If they are phone numbers then explicity call it 'getPhoneNumbers'.
Though I'm not entirely sure I understand what you asking, I think this may solve your issues:
for(int i = 0; i < contactos.size(); i++) {
Contacto next = contactos.get(i);
if(next.getEmails().contains(m)) {
next.display();
}
}
And as an afterthought, is there any specific reason you're only checking string containment? I would suggest that you check case-insensitive equality unless you really do want to find out if the string just contains the element.
Is this what you are looking for?
public class EmailSearcher {
Reader reader = new KeyboardReader();
public EmailSearcher(ArrayList<Contacto> contactos){
while(reader.read() != 'keyThatTerminates') {
String m = reader.read();
for(int i = 0; i<contactos.size();i++){
var row = contactos.get(i);
if(row.getEmails().contains(m)){
row.display();
}
}
}
}
}
I'm translating a Java program into X10 and have run into a couple problems that I was wondering if anyone could help me translate.
Here's one Java segment that I'm trying to translate:
ArrayList<Posting>[] list = new ArrayList[this.V];
for (int k=0; k<this.V; ++k) {
list[k] = new ArrayList<Posting>();
}
And here's what I've done in X10:
var list:ArrayList[Posting]=new ArrayList[Posting](this.V);
for (var k:int=0; k<this.V; ++k) {
list(k)=new ArrayList[Posting]();
}
The line that's generating a mess of error statements is this:
list(k)=new ArrayList[Posting]();
Any suggestions and maybe an explanation on what I'm doing wrong?
Agreed with trutheality. You need to define list as something like Rail[ArrayList[Posting]] :
var list:Rail[ArrayList[Posting]]=new Rail[ArrayList[Posting]](this.V);
Also, as X10 supports type inference for immutable variables, it's often better to use val instead of var and omit the type declaration altogether:
val list = new Rail[ArrayList[Posting]](this.V);
Here is code that should work for you:
val list = new Rail[ArrayList[Posting]](this.V);
for (k in 1..(this.V)) {
list(k)=new ArrayList[Posting]();
}
And you can also do
val list = new Rail[ArrayList[Posting]](this.V, (Long)=>new ArrayList[Temp]());
i.e. use a single statement to create an initialized array.
Can someone please give the Java equivalent of the below python (which slices a given array into given parts) which was originally written by ChristopheD here:
def split_list(alist, wanted_parts=1):
length = len(alist)
return [ alist[i*length // wanted_parts: (i+1)*length // wanted_parts]
for i in range(wanted_parts) ]
I don't know any python but can really use the above code in my Java app. Thanks
Maybe something like this:
List<List<T>> splitList(List<T> alist, int wantedParts) {
ArrayList<List<T>> result = new ArrayList<List<T>>();
int length = alist.length;
for (int i = 0; i < wantedParts; i++) {
result.append(alist.subList(i*length/wantedParts,
(i+1)*length/wantedParts));
}
return result;
}
If your alist will be structurally modified later in any way, you will have to make a copy of the sublist created by the subList method within the code, otherwise the results will be unpredictable.
Don't reinvent the wheel, the google collections api has a function called partition which does precisely that
In my Java SWT application I'm hosting an 3rd party ActiveX control. I'm using OleClientSite to do this.
// Ah, this works. :-)
OleAutomation comObject = new OleAutomation(...);
There are 2 easy little functions I want to call from Java. Here are the COM function definitions:
[id(5)]
void easyFoo([in] int blah);
[id(20)]
void problemFoo([in] VARIANT floatArray);
Easy, right? Here's my pretend code:
// Ah, this works. :-)
OleAutomation comObject = new OleAutomation("Some3rdPartyControlHere");
// Call easyFoo(42). This works. :-)
int easyFooId = 5;
comObject.invoke(easyFooId, new Variant[] { new Variant(42) });
// Call problemFoo(new float[] { 4.2, 7.0 }). This doesn't work. :-(
int problemFooId = 20;
comObject.invoke(problemFooId, [ACK! What goes here?]);
The problem is on the last line: how do I pass a float array to the 3rd party COM object? HELP!
You need to pass a float array. In COM terms, that mean s a VARIANT with vt set to VT_R4|VT_ARRAY. An array of variants may not work as the document does not say it can accept an array of variants (VT_VARIANT |VT_ARRAY). In java you should be able to use float[] as the parameter type. If not you can always call the Windows API to construct a safe array of desired type.
I suspect there is no constructor that takes a float[] because VARIANTs don't have a float array member.
I think what you need to do to make this work is pack up your floats into a SAFEARRAY (ick; and I have no idea how to create one in Java).
Alternatively, you may try serializing your array to raw bits and use the BYTE* member of the VARIANT struct, and pass an int that has the count of bytes so you can accurately de-serialize on the other side (and I assume this is all in the same process and thread, otherwise it gets harder).
[id(20)]
void problemFoo([in] VARIANT bytes /* VT_BYREF|VT_UI1 */, [in] VARIANT byteCount /* VT_UI4 */);
What's wrong with creating an array of Variant and filling it with your float array values?
Variant[] problemFooArgs = new Variant[myFloats.length];
for( int i=0; i<myFloats.length; i++)
{
problemFooArgs[i] = new Variant(myFloats[i]);
}
If it really needs only one argument (an array of float), you could try one level of indirection:
Variant[] problemFooArgs = new Variant[1];
Variant[] myFooArgs = new Variant[1];
for( int i=0; i<myFloats.length; i++)
{
myFooArgs [i] = new Variant(myFloats[i]);
}
problemFooArgs[0] = myFooArgs;
If the simple approach does not work and you do need a SAFEARRAY, you could try and create one after the example "Reading and writing to a SAFEARRAY", using the constants of org.eclipse.swt.internal.win32.OS. But it seems for char[] only.
Other source of inspiration for creating the relevant SAFEARRAY:
class SafeArray of project com4j (and its associated class, like Variant)