How to overwrite an array?
I am trying to overwrite the values in an array, but I am getting an ArrayIndexOutofBoundsException.
This is how I am attempting to do it:
if ((checkTempEnergy(tempDistance, numIons, Q)) < (checkEnergy(distance, numIons, Q))) {
for (int i=0; i < numIons; i++) {
arrayX[i] = tempX;
arrayY[i] = tempY[i];
arrayZ[i] = tempZ[i];
distance[i] = tempDistance[i];
The code compiles. Both arrays are the same size and declared the same way. The only difference is the name. Any help would be greatly appreciated, and I will award the 10 DUKE Stars.
DrLaszloJamf wrote:
You've been posting this question over and over and the drill should be familiar to you by now:
1. Posting a snippet like that isn't really helpful to forum readers. For one thing, the fact that your code has such a trivial error in it makes you an unreliable reporter. Why should we believe what you claim?Ok, but the program is not complete, repeats itself, and looks awful. It is compilable though.
public class FormingSalt2
//creates a cube for the salt to form
public static double volCube (double side) {
double volume = side*side*side;
return (volume);
//create a random coordinates within the cube
public static double randomCoord (double side) {
double ranCoord = (double) (Math.random() * (side-3.76));
return (ranCoord);
//check the distances between ions but without double checking and without checking the distances between the same ions.
public static double[] checkDistance (double arrayX[], double arrayY[], double arrayZ[], double distance[], int numIons) {
//check the distances
int l = 0;
for (int i=1; i<numIons; i++) {
for (int j=0; j<i; j++){
double dx = arrayX[i] - arrayX[j];
double dy = arrayY[i] - arrayY[j];
double dz = arrayZ[i] - arrayZ[j];
distance[l] = Math.sqrt((dx*dx) + (dy*dy) + (dz*dz));
//System.out.println(tempDistance[l]+" is the distance "+ l);
l++;
return distance;
//check the temporary distances between the randomly moved ions but without double checking and without checking the distances between the same ions.
public static double[] checkTempDistance (double tempX[], double tempY[], double tempZ[], double tempDistance[], int numIons) {
//check the distances
int l = 0;
for (int i=1; i<numIons; i++) {
for (int j=0; j<i; j++){
double dx = tempX[i] - tempX[j];
double dy = tempY[i] - tempY[j];
double dz = tempZ[i] - tempZ[j];
tempDistance[l] = Math.sqrt((dx*dx) + (dy*dy) + (dz*dz));
//System.out.println(tempDistance[l]+" is the distance "+ l);
l++;
return tempDistance;
//checks the Total Energy between the ions
public static double checkEnergy (double distance[], int numIons, double Q[]) {
final double EPSILON = Math.sqrt(35.6*120), SIGMA = ((2.75+3.41)/2), K1 = 2.30, K2 = 0.0000552;
double E1=0, E1sum=0, E2=0, E2sum=0;
for (int i=1; i<numIons; i++) {
for (int j=0; j<i; j++) {
E1 = (K1) * ((Q[j]*Q)/distance[j]);
E1sum = E1sum + E1;
//System.out.println(E1);
E2 = (K2)*EPSILON* (Math.pow((SIGMA/distance[j]),12) - Math.pow((SIGMA/distance[j]),6));
E2sum = E2sum + E2;
//System.out.println(E2);
//System.out.println();
//System.out.println(E1sum);
//System.out.println();
//System.out.println(E2sum);
//System.out.println();
double ETotal = (E1sum + E2sum) * Math.pow(10,-18);
return ETotal;
//checks the Total Temporary Energy between the randomly placed ions
public static double checkTempEnergy (double tempDistance[], int numIons, double Q[]) {
final double EPSILON = Math.sqrt(35.6*120), SIGMA = ((2.75+3.41)/2), K1 = 2.30, K2 = 0.0000552;
double E1=0, E1sum=0, E2=0, E2sum=0;
for (int i=1; i<numIons; i++) {
for (int j=0; j<i; j++) {
E1 = (K1) * ((Q[j]*Q[i])/tempDistance[j]);
E1sum = E1sum + E1;
//System.out.println(E1);
E2 = (K2)*EPSILON* (Math.pow((SIGMA/tempDistance[j]),12) - Math.pow((SIGMA/tempDistance[j]),6));
E2sum = E2sum + E2;
//System.out.println(E2);
//System.out.println();
//System.out.println(E1sum);
//System.out.println();
//System.out.println(E2sum);
//System.out.println();
double ETotal = (E1sum + E2sum) * Math.pow(10,-18);
return ETotal;
//MAIN METHOD!
public static void main(String[] args)
double side=0;
//Create the Cube and ask the user for how many Ions.
//Number of Ions must be even for this program to converge/work.
while (side<1.89){
System.out.println("Choose length of one side of the Cube in Angstroms:");
side = StdIn.readDouble();
if (side<1.89){
System.out.println("Side must be greater than Van der Waals radius of Argon, which is 1.88A.");
System.out.println();
System.out.println("The Volume of the Cube is: "+volCube(side));
System.out.println();
System.out.println("How many ions should there be in the Cube? Note: Odds are Na+ and Evens are Cl-");
int numIons = StdIn.readInt();
System.out.println();
//Initialize and allocate memory for the Arrays...
double arrayX[] = new double[numIons];
double arrayY[] = new double[numIons];
double arrayZ[] = new double[numIons];
double tempX[] = new double[numIons];
double tempY[] = new double[numIons];
double tempZ[] = new double[numIons];
int k=0;
for (int j=1; j<numIons; j++){
for (int i=0; i<j; i++) {
k++;
double distance[] = new double[k];
double tempDistance[] = new double[k];
double Q[] = new double[numIons];
for (int i=0; i<numIons; i++){
Q[i] = Math.pow(-1, (i+2));
//Creates the initial random set of (x,y,z) coordinates
for (int i=0; i != numIons; i++) {
arrayX[i] = randomCoord(side);
arrayY[i] = randomCoord(side);
arrayZ[i] = randomCoord(side);
System.out.println("Coordinates for ion "+(i+1)+" are: ");
System.out.println("("+ arrayX[i]+ ","+ arrayY[i]+","+ arrayZ[i]+")");
//check the distances between the ions
System.out.println();
System.out.println("Distances are: ");
checkDistance(arrayX, arrayY, arrayZ, distance, numIons);
int l=0;
for (int i=0; i<k; i++){
System.out.println(distance[l]+" is the distance "+ l);
l++;
System.out.println();
//check the energy between the ions in their initial position
System.out.println(checkEnergy(distance, numIons, Q)+" is in Joules!");
System.out.println();
//ask the User how many Trials
System.out.println("How many Trials?");
int numTrials = StdIn.readInt();
System.out.println();
//Create a new random set of (x,y,z) coordinates by taking a random Step between -1.88 and 1.88 Angstroms
for (int i=0; i<numIons; i++) {
double tinyStepX = ((double)(Math.random()*(3.76))- 1.88); //adds a random step between -1.88 and 1.88 Angstroms
double tinyStepY = ((double)(Math.random()*(3.76))- 1.88);
double tinyStepZ = ((double)(Math.random()*(3.76))- 1.88);
tempX[i] = arrayX[i] + tinyStepX;
tempY[i] = arrayY[i] + tinyStepY;
tempZ[i] = arrayZ[i] + tinyStepZ;
System.out.println("Coordinates for ion "+(i+1)+" are: ");
System.out.println("("+ tempX[i]+ ","+ tempY[i]+","+ tempZ[i]+")");
//check the distances between the ions in the new positions
System.out.println();
System.out.println("Distances are: ");
checkTempDistance(tempX, tempY, tempZ, tempDistance, numIons);
l=0;
for (int i=0; i<k; i++){
System.out.println(tempDistance[l]+" is the distance "+ l);
l++;
System.out.println();
//check the energy of the randomly moved ions
System.out.println(checkTempEnergy(tempDistance, numIons, Q)+" is in Joules!");
System.out.println();
if ((checkTempEnergy(tempDistance, numIons, Q)) < (checkEnergy(distance, numIons, Q))) {
for (int i=0; i < numIons; i++) {
arrayX[i] = tempX[i];
arrayY[i] = tempY[i];
arrayZ[i] = tempZ[i];
distance[i] = tempDistance[i];
System.out.println(checkEnergy(distance, numIons, Q)+" is in Joules!");
System.out.println();
2. Once again, if you report an error you should indicate clearly the line that raises it. Once again you have failed to do this.Ok, my bad. The error is in line 200.
3.. One day you will realize the best way to get help is to write a SSCCE: [http://mindprod.com/jgloss/sscce.html]
My outputs are supposed to be random, so every output is different. But here is an example:
[http://i285.photobucket.com/albums/ll52/RommelTJ/FormingSalt2_help.jpg|http://i285.photobucket.com/albums/ll52/RommelTJ/FormingSalt2_help.jpg]
I am really sorry if I offended you or annoyed you in any way. I am just trying to get help and be nice. :(
Edited by: RommelTJ on Jun 18, 2008 2:38 PM
Similar Messages
-
How to map an array to fixed fields using Biztalk mapper
I need to remap an array of objects like this:
<Root>
<ListOfObjs>
<Obj>
<Attr1>0000</Attr1>
<Attr2>Hello!</Attr2>
</Obj>
<Obj>
<Attr1>1111</Attr1>
<Attr2>Hello1!</Attr2>
</Obj>
</ListOfObjs>
</Root>
in an output like this:
<Root>
<Obj1_Attr1>0000</Obj1_Attr1>
<Obj1_Attr2>Hello!</Obj1_Attr2>
<Obj2_Attr1>1111</Obj2_Attr1>
<Obj2_Attr2>Hello1!</Obj2_Attr2>
</Root>
So in my XSD schema I have something like this:
Schema Input
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="ListOfObjs">
<xs:complexType>
<xs:sequence>
<xs:element name="Obj">
<xs:complexType>
<xs:sequence>
<xs:element name="Attr1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Attr2">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
Schema output
<xs:element name="Root">
<xs:complexType>
<xs:sequence>
<xs:element name="Obj1_Attr1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Obj1_Attr2">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Obj2_Attr1">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
<xs:element name="Obj2_Attr2">
<xs:simpleType>
<xs:restriction base="xs:string">
<xs:minLength value="1"/>
<xs:maxLength value="50"/>
</xs:restriction>
</xs:simpleType>
</xs:element>
</xs:sequence>
</xs:complexType>
</xs:element>
In addiction I have to evaluate every single value because when I found some conditions (like if value=0000 output should be NULL).
What would be the best way to do it? I'm thinking to develop a custom functoid but I'm not sure it would be the best way, probably it could be done even using XSLT inline transforms, can you point me in the best direction?
Thank youHi,
You cannot directly map an array output to any single field in BizTalk mapper.
Couple of options :
1) create
the Xslt or inline C# code
Refer:
http://seroter.wordpress.com/2008/10/07/splitting-delimited-values-in-biztalk-maps/
2) Shankycheil has
provided a solution to similar requirement in the below link, u can also refer that.
https://social.msdn.microsoft.com/Forums/en-US/55ec472d-4f34-4057-b1c6-0e50740f0f6e/how-to-itterate-string-array-values-in-biztalk-mapper?forum=biztalkgeneral
Rachit
Thank you, I already seen both posts, but I'm not sure they are what I need or I can't understand well how to use them.
Speaking about the first solution, as I told before, in the example I should have an array already formed and delimited by a char (something like "obj1attr1-obj1attr2-ob2attr1-obj2attr2". In this situation probably this example could be a good
point to start from, but how to transform my complex input object in a similar formatted string?
About the second I don't understand well what is the working solution that they have adopted. Is the 4 steps solution suggested by Shankycheil? If yes, how can I loop between all array elements and extract all their values? -
How to build a array with high sampling rates 1K
Hi All:
Now I am trying to develop a project with CRio.
But I am not sure how to build a array with high sampling rates signal, like >1K. (Sigle-point data)
Before, I would like to use "Build Arrary" and "Shift Register" to build a arrary, but I found it is not working for high sampling rates.
Is there anyother good way to build a data arrary for high sampling rates??
Thanks
Attachments:
Building_Array_high_rates.JPG 120 KBCan't give a sample of the FPGA right now but here is a sample bit of RT code I recently used. I am acquiring data at 51,200 samples every second. I put the data in a FIFO on the FPGA side, then I read from that FIFO on the RT side and insert the data into a pre-initialized array using "Replace Array subset" NOT "Insert into array". I keep a count of the data I have read/inserted, and once I am at 51,200 samples, I know I have 1 full second of data. At this point, I add it to a queue which sends it to another loop to be processed. Also, I don't use the new index terminal in my subVI because I know I am always adding 6400 elements so I can just multiply my counter by 6400, but if you use the method described further down below , you will want to use the "new index" to return a value because you may not always read the same number of elements using that method.
The reason I use a timeout of 0 and a wait until next ms multiple is because if you use a timeout wired to the FIFO read node, it spins a loop in the background that polls for data, which rails your processor. Depending on what type of acquisition you are doing, you can also use the method of reading 0 elements, then using the "elements remaining" variable, to wire up another node as is shown below. This was not an option for me because of my programs architecture and needing chunks of 1 second data. Had I used this method it would have overcomplicated things if I read more elements then I had available in my 51,200 buffer.
Let me knwo if you have more qeustions
CLA, LabVIEW Versions 2010-2013
Attachments:
RT.PNG 36 KB
FIFO read.PNG 4 KB -
How to set an array element in an object type of array??
Hi,
I have set attribute as follow:
Color[] colors;
Color[] colors = new Color[20];
colors[0] = "Red";
colors[1] = "blue";I can't compile this code. It said:
"Incompatible type -found java.lang.String but expected Colors
Could you tell me how to set an array elements when the array type is an object?in your case, the array shouldn't be Color[] but String[] since you re intending to put strings into it
by the way, you could also use a hashmap to map a string to a color:
HashMap<String, Color> hm = new HashMap<String, Color>();
hm.put("Red", Color.RED);
hm.put("Blue", Color.BLUE);
etc. -
How to add byte[] array based Image to the SQL Server without using parameter
how to add byte[] array based Image to the SQL Server without using parameter.I have a column in table with the type image in sql and i want to add image array to the sql image column like below:
I want to add image (RESIM) to the procedur like shown above but sql accepts byte[] RESIMI like System.Drowing. I whant that sql accepts byte [] array like sql image type
not using cmd.ParametersAdd() method
here is Isle() method contentSQL Server binary constants use a hexadecimal format:
https://msdn.microsoft.com/en-us/library/ms179899.aspx
You'll have to build that string from a byte array yourself:
byte[] bytes = ...
StringBuilder builder = new StringBuilder("0x", 2 + bytes.Length * 2);
foreach (var b in bytes)
builder.Append(b.ToString("X2"));
string binhex = builder.ToString();
That said, what you're trying to do - not using parameters - is the wrong thing to do. Not only it is insecure due to the risk of SQL injection but in the case of binary data is also inefficient since these hex strings are larger than the original byte[]
data. -
How to get byte array from jpg in resource for Image XObject?
Hi,
Does anyone know how to get an array of bytes from a jpg from resource without an external library except MFC?
The array of bytes is needed to create an Image XObject so it can be included in an appearance for an annotation.Sounds like a standard Windows programming question, not specific to the SDK.
-
How to list an array of objects using struts in jsp and ActionForm
I am using the struts ActionForm and need to know how to display an Array of objects. I am assuming it would look like this in the ActionForm:
private AddRmvParts addRmv[];
But I am not sure how the getter and setter in the ActionForm should look. Should it be a Collection or an Iterator?
I am thinking I need to use an iterator in the jsp page to display it.
I am also wondering if the AddRmvParts class I have can be the same class that gets populated by the retrieval of data from the database. This class has 10 fields that need to display on the jsp page in 1 row and then multiple rows of these 10 fields.
Thanks.Most probably the easiest way to make it accessible in your page is as a collection.
ie
public Collection getAddRmvParts()
You can use the <logic:iterate> or a <c:forEach>(JSTL) tag to loop through the collection
As long as the individual items in the collection provide get/set methods you can access them with the dot syntax:
Example using JSTL:
<c:forEach var="addRmvPart" items="${addRmvParts}">
<c:out value="${addRmvPart.id} ${addRmvPart.name} ${addRmvPart.description}"/>
</c:forEach> -
Labview How to specify 1d array of clusters as data types for variant to data
Hi, I'm new to labview. Can anyone tell me how to specify 1d array of clusters as data types for variant to data?
First of all, you should be sure that there is such a data type within the variant; otherwise, you will run into errors.
I recommend you to create the cluster and create a type definition from it. Then drop an array shell from the array palette and drop the cluster type into that array.
Connect that constant to the data type input of the Variant To Data function.
Norbert
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it. -
How to Display string array in jsp page using netui-data:repeater tag ??
hi,
I am trying to display a string array in a table using the netui-data:repeater tag.
I have to use a page flow controller Array(1 Dimensional) to be displayed in the jsp.
Can any one tell me how to print the array in a table of 3rows & 5 columns.
Here is the code on which I am crrently working on.
<netui-data:repeater dataSource="{pageFlow.strWorkObject_Array}">
<netui-data:repeaterHeader>
<table cellpadding="4" border="1" class="tablebody">
</netui-data:repeaterHeader>
<netui-data:repeaterItem>
<tr>
<td><netui:label value="{container.item}" >
</netui:label></td>
<td><netui:label value="{container.item}">
</netui:label></td>
<td><netui:label value="{container.item}">
</netui:label></td>
</tr>
</netui-data:repeaterItem>
<netui-data:repeaterFooter>
</table>
</netui-data:repeaterFooter>
</netui-data:repeater>weblogic.developer.interest.workshop
Mansoor Naseem wrote:
I would like to know where the pageflow newsgroup is.
These are all the groups in weblogic.developer.interest:
weblogic.developer.interest.60beta.* (5 groups) weblogic.developer.interest.management
weblogic.developer.interest.61beta.* (2 groups) weblogic.developer.interest.misc
weblogic.developer.interest.clustering.* (1 group) weblogic.developer.interest.performance
weblogic.developer.interest.commerce weblogic.developer.interest.personalization
weblogic.developer.interest.ejb.* (3 groups) weblogic.developer.interest.portal
weblogic.developer.interest.environment weblogic.developer.interest.rmi-iiop
weblogic.developer.interest.jdbc weblogic.developer.interest.security
weblogic.developer.interest.jms weblogic.developer.interest.servlet
weblogic.developer.interest.jndi weblogic.developer.interest.tools
weblogic.developer.interest.jsp weblogic.developer.interest.weblogicenterprise
MN -
How to create an array using reflection.
How to create an array using reflection.
I want to achive something like this,Object o;
o = (Object)(new TestClass[10]);but by use of reflection.
To create a single object is simple:Object o;
o = Class.forName("TestClass").newInstance();But how do I create an array of objects, when the class of objects is known only by name? (Can't use Object[] because even though an Object[] array can be filled with "TestClass" elements only, it Cannot be casted to a TestClass[] array)
Anybody knows?":-)
Ragnvald Barth
Software enigneerFound it!
the java.lang.reflect.Array class solves it!
Yes !!! -
How to create an array of linklists??? ... plz
Hi guys,
I am trying to create a Hash table. My link list is already working. But i am still confused about how to create any array linklist for solving the collisions. Do i need to create a new instance of the linklist for each array slot? What is the better way to do?
just need some hint to start it.
abdulYes, you need to create a new instace for each slot.
LinkedList[] ht = new LinkedList[size];
for(int x = 0; x < size; x++)
ht[x] = new LinkedList(); -
How to create an array variable
Hi folks,
I'm developing a flowN activity , for each branch created, I need to pass different values (values I get from DB).
For this my plan would be,
-create an array variable , load all the values(from DB) into the array
-pass the array to each branch in flowN activity based on the index
As I'm newbie to BPEL, I do not know how to create an array variable.
Could you people guide me please.
Regards
VikiHi,
I created my string array like
<element name="string_array">
<complexType>
<sequence>
<element name="input" type="string" minOccurs="0" maxOccurs="unbounded"/>
</sequence>
</complexType>
</element>
<variable name="arr" element="client:string_array"/>
I am able to add values to my array variable named as "arr"
When I use the following code snippet, it gives my the value of the entire list.
<%ora:getNodeValue(bpws:getVariableData('arr'))%>
But when I try retireve values one by one like <%ora:getElement('arr','/client:string_array/client:input',[0])%>, its giving me error.
Guide to get throu' this.
Viki -
How to populate an array with random text files.
I am making a Jeopardy program. I have my program set up so that it retrieves 5 random text files. I just want to know how I populate one array with all the lines from the text files my program is retrieving.
You can read a textfile line by line and add each line to an ArrayList. An ArrayList is very much like an array only that it's "open ended". You can start adding lines without first knowing how many you're going to get. If you still want an ordinary "static" array when you're finished reading lines you can easily get one from the ArrayList and then drop the ArrayList.
-
How to create 2D array with 3 rows and unlimit column?
how to create 2D array with 3 rows and unlimit column?
Here are images of what I described in my previous post
Message Edited by JoeLabView on 11-14-2007 07:56 AM
Attachments:
2D-array_code.PNG 7 KB
2D-array_values.PNG 13 KB -
How to use OracleDbType.Array provided by ODP with User defined type ?
Can anyone help me how to use OracleDbType.Array provided by ODP.NET ?
I need to pass string array to a oracle stored procedure .
User defined array type defined in oracle is :
CREATE TYPE TYPE_NAME IS TABLE OF varchar2(20) ;
This type is defined outside of any package , and i have tested that if definition of type is modified to
CREATE TYPE TYPE_NAME IS TABLE OF varchar2(20) index by binary_integer , i am able to pass array as AssociativeArray to my Stored Procedure.
But how to pass array object if the Type's definition does not contain index by clause ?
Please help how to pass Array object to Oracle Stored Procedure ?The solution described in Passing Array of UDT or Collection as IN OUT using OracleDbType.InputOutput
is working for me.
Edited by: stzueger on Jan 2, 2012 10:32 AM
Maybe you are looking for
-
How much time does a single transmission take?
Hello, I was wondering what is the time taken to transmit one sample takes. For example, if the sample interval is 5 seconds, does the actual transmission take 10ms? What does it do for the rest of the time? Also, how much energy does a 3202 node tak
-
Drill down issue in excel add-in
When I double click on excel for drill down it is going into loop for long...please help. RJ
-
Microphone cutting out periodically
My microphone periodically turns off during skype calls, and only during them. If I am not in a skype call all my other programs can use my mic fine, but after about 10 minutes in a call my mic will turn off for my entire computer. I don't even have
-
Mx.controls.Alert.show windowHeight
Hello All I am trying to set windowHeight for my alert component. Below a snippet but does not seem to work any suggestion greatly appreciated Adam var myAlert; myAlert = mx.controls.Alert.show("Please select:\n"+msg+"","Info", {windowHeight: 400});
-
JMF occupies too much CPU resource in two cases
Hi, I am using JMF to develop a RTP streaming application including both client(RTPReceiver) and server(RTPTransmitter). And I notice that my application will consum unusually high CPU resource in the following two situations. Any one have any idea o