Declaring an array that is populated with a for loop
Hi Guys,
i have an array declared like this at the top of my class:
private int [] longTable ={16,17,18,19,20,21,22};
It gets accessed from within methods in the same class.
I want to extend this array to go from 16 up to 124 in increments of 1, and do not want to hard code those values,
I tried this code, but its giving errors including longTable cannot be resolved to a type...
private int [] longTable = new longTable[125];
for(int i=16; i<longTable.length; i++)
longTable[i] =i;
how should i go about this?
I think you have to write every number til 124, because you cannot "change" the size of this array after declaration.
try this then:
private int[] something = new int[125];
int at = 0;
for(int i = 16; i < something.length; i++)
something[at] = i;
at++;
This will hopefully give you a array holding 125 int numbers
something[0] = 16
something[1] = 17
something[2] = 18
something[3] = 19
.... etc til 125
is this what you wanted ?
Edited by: hdMilo on Mar 26, 2008 7:51 AM
Similar Messages
-
I need help with my for loop in this array
Ok well, I can't get my code to work. Also, please remember that this is just my draft so it isnt pretty. I will fix it up later so please look at it. The thing I want to do is look into the array for a time that matches what the user entered and return the toString() of that one. I know there is something wrong with my for loop but I cant figure how to fix it. please help. here is what i have so far:
import javax.swing.JOptionPane;
public class Runner
public static void main (String[] args)
String timeStr;
int time, again, optiStr;
Inbound[] in = new Inbound[25];
in[0]=new Inbound ("",0,"On Time num0");
in[1]=new Inbound ("",2,"On Time num1");
in[2]=new Inbound ("",3,"Delayed num2");
in[3]=new Inbound ("",4,"On Time");
in[4]=new Inbound ("",5,"On Time");
in[5]=new Inbound ("",6,"Canceled");
in[6]=new Inbound ("",1,"Canceled num6");
in[7]=new Inbound ("",8,"On Time");
in[8]=new Inbound ("",9,"Delayed");
in[9]=new Inbound ("",10,"On Time");
in[10]=new Inbound ("",11,"Delayed");
in[11]=new Inbound ("",12,"On Time");
in[12]=new Inbound ("",13,"Delayed");
in[13]=new Inbound ("",14,"On Time");
in[14]=new Inbound ("",15,"On Time");
in[15]=new Inbound ("",16,"On Time");
in[16]=new Inbound ("",17,"Canceled");
in[17]=new Inbound ("",18,"On Time");
in[18]=new Inbound ("",19,"On Time");
in[19]=new Inbound ("",20,"Canceled");
in[20]=new Inbound ("",21,"On Time");
in[21]=new Inbound ("",22,"Delayed");
in[22]=new Inbound ("",23,"On Time");
in[23]=new Inbound ("",24,"Cancled");
in[24]=new Inbound ("",7,"On Time num24");
do{
timeStr = JOptionPane.showInputDialog ("In military time, what hour do you want?");
time = Integer.parseInt(timeStr);
if (time<=0 || time>24)
JOptionPane.showMessageDialog (null, "Error");
optiStr = JOptionPane.showConfirmDialog (null, "If you want Incoming flights click Yes, but if not click No");
if (optiStr==JOptionPane.YES_OPTION)
//(ok this is the for loop i am talking about )
for (int index = 0; index < in.length; index++)
if ( time == Inbound.getTime())
JOptionPane.showMessageDialog (null, Inbound.tostring()); //return the time asked for
// else JOptionPane.showMessageDialog (null, "else");
}//temp return else if failed to find time asked for
// else
// if (optiStr==JOptionPane.CANCEL_OPTION)
// JOptionPane.showMessageDialog(null,"Canceled");
// else
// {Outbound.run();
// JOptionPane.showMessageDialog (null, "outbound");}//temp
again=JOptionPane.showConfirmDialog(null, "Try again?");
while (again==JOptionPane.YES_OPTION);
}any help would be greatly appriciated.rumble14 wrote:
Ok well, I can't get my code to work. Also, please remember that this is just my draft so it isnt pretty. I will fix it up later so please look at it. The thing I want to do is look into the array for a time that matches what the user entered and return the toString() of that one. I know there is something wrong with my for loop but I cant figure how to fix it. please help. here is what i have so far:
>//(ok this is the for loop i am talking about )
for (int index = 0; index < in.length; index++)
if ( time == Inbound.getTime())
JOptionPane.showMessageDialog (null, Inbound.tostring()); //return the time asked for
Inbound.getTime() is a static method of your Inbound class, that always returns the same value, I presume? As opposed to each of the 25 members of your array in, which have individual values?
Edited by: darb on Mar 26, 2008 11:12 AM -
Help! (please I have a 1GB iPod that's loaded with music for which my computer can no longer find the original files-due to external mass storage crash during recent move. Is there a way to import iPod music back into my iTunes library on computer???
Or If there is any purchased music then you can try to transfer purchases
http://support.apple.com/kb/ht1848
If you're in the US you can reload purchased music
http://support.apple.com/kb/ht2519 -
the demand of my application is that i can not replace for loop with a while loop.because i need fixed number of iterations and as far as i know fixed iterations could be only with possible with the for loop.
your recommended second option that i could add true/false case.
this true/false case must be inside the for loop or outside the for loop?if this case is inside the for
loop, how can i send stop command from outer while
loop?
more over do you have any example for this please?
thanks"You can execute a fixed number of iterations using a while loop by comparing the iteration count to the number of iterations you want and wiring the output of that comparison (e.g. Less Than or Equal To) to the continue (or stop) terminal of your while loop. Which comparison you use depends on personal preference, where you wire the desired count and the interation count, and whether you're using the while loop as Continue if True or Stop if True.
Ben gave you step-by-step instructions in response to your previous question. Look here for Ben's response.
Ben's response looks pretty good and detailed to me. It certa
inly deserved better than a 1-star rating. -
Trying to save key strokes with a for loop
I have been trying to action script for years with no
success. I to some time off and learned javascript and some oop
(ruby) so I thought I would take another stab at it. I am still
having problems. Here goes...
I have several movieclips on the stage that will be set up as
buttons. They all have the same effects associated with mouseover
and mouseout so I thought I could save some key strokes with a for
loop. But I can't get it working right. Could someone tell me what
I am doing wrong please?
If anyone can help it would be appreciated.kglad,
Thanks for taking a look. In answer to your question. The
movie clip instance names are mc_ItemText0 ... mc_ItemText3.
quote:
those declarations at the top of your for-loop aren't doing
anything useful. what are the names of you movieclips that are
on-stage?
In reg\ards to the variable declarations, sorry my variable
names are causing confusion. I use the name "mc" for two different
variables. Once for the FOR loop and a separate time locally in the
functions grow and shrink. The ones set for the For loop are used
in the 'onEvents' at the bottom of the script. Sorry for the
confusion.
Because the code works if I take it out of the FOR loop and
replace the
's with the numbers that corruspond to the movie clip; I don't
think the similar names are conflicting because of the scope. To
make the code more readable and eliminate confusion I have posted
the code with unique variable names.
I have searched the forum for FOR loop difficulties and
found several cases that seemed similar to mine, but I didn't
understand what was happening.
Examples:
http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=15&catid=288&threadid =1253110&highlight_key=y&keyword1=for%20loop
http://www.adobe.com/cfusion/webforums/forum/messageview.cfm?forumid=15&catid=288&threadid =1202338&highlight_key=y&keyword1=for%20loop
My code with unique variable names to end confusion: -
Issues with nested for loops - saving images from a camera
Hi all,
I've written a vi. to capture a specific number of images ('Image No') and save these images, outputted to a folder of my choice. Each image is identified sequentially. However, I wish to do a number of iterations ('Run') of this capture sequence, such that the filename of each image would be 'Filename (Run)_(Image No).png', e.g. run 5, image 10 would be 'Filename 5_10.png'. I have tried a nested for loop for this but I receive an error 'Asynchronous I/O operation in progress' (I've attached a printscreen).
Can anyone assist me in solving this problem? I preiously posted this in machine Vision but got no response (http://forums.ni.com/t5/Machine-Vision/Capturing-image-sequences-issues-with-nested-for-loops/m-p/19...). Please find attached my vi.
Kindest regards and thanks,
Miika
Solved!
Go to Solution.
Attachments:
Labview problem.jpg 3841 KB
Image sequence save to file.vi 48 KBMiika,
the problem is not the filenam, but the name of the folder (AHHHHH!). You try to create the same folder in the outer for loop over and over again.... (it is the error message above the '======', not below )
Norbert
CEO: What exactly is stopping us from doing this?
Expert: Geometry
Marketing Manager: Just ignore it. -
How to traverse an array with a for-loop in this certain way...
so I need to traverse an array starting from the first element, then from the last element, then the second element, then the second last element, then third element and so on... in a for loop
how would that look in code?
Any help would be appreciated, thanks!
Edited by: inspiredone on Apr 4, 2008 6:34 AMWow, that's a really clever and elegant way to do it promethuzz. I actually came up with something already before I read your post so yeah don't worry about me copying or anything. Mine is not as elegant/efficient and I definitely like yours a lot more but I will just share it anyway...it looks pretty similar to the second algorithm The_Matrix has provided. I figured this out from thinking how quicksort compares its pivot with the two indexes...
int front=0;
int back = a.length - 1;
int counter =0;
boolean switchflag = true;
while(front<=back)
System.out.println(a[counter]);
if(switchflag)
counter = back;
front++;
switchflag = false;
else
counter = front;
back--;
switchflag = true;
} Anyways, thanks a lot for you guys help, promethuzz, The_Matrix, and newark.
Edited by: inspiredone on Apr 4, 2008 11:25 AM
Edited by: inspiredone on Apr 4, 2008 11:27 AM -
Please help with the FOR loop and the array..
I was trying to place some words in the Movie Clip
"TextPanel" and set a
random position to each of them.
But it's not working.
1) I created a Movie Clip "word" and inside that MC I created
a text field
and gave it an identifier "textFiled".
2) The linkage name for Movie Clip "word" I set to "word".
3) In the actionscript I created an Array called "aWords".
4) Then I created a FOR loop that should
place (attach) Movie Clips "word0", "word1", "word2" and
"word3" to the
movie clip TextPanel, and set the textField text for each of
them to the
text from the Array.
But the script attaches 4 Movie Clips with a name
"Undefined", instead of 4
different names (from the Array).
What is wrong with this script?
var aWords:Array = [apple,banana,orange,mango];
for(i=0;i<aWords.length;i++){
var v = TextPanel.attachMovie("word","word"+i,i);
v.textFiled.text = aWords
v._x = randomNumber(0,Stage.width);
v._y = randomNumber(0,Stage.height);
Thanks in advanceBut in my Post I already wrote v.textFiled.text = aWords
so I don't understand what were you correcting..
And one more:
I have tested it by changing the
v.textFiled.text = aWords; to v.textFiled.text = "some
word";
and it's working fine.
So there is something wrong with the Array element, and I
don't know why..
"aniebel" <[email protected]> wrote in
message
news:ft2d5k$lld$[email protected]..
> Change:
> v.textFiled.text = aWords;
>
> to:
> v.textFiled.text = aWords
>
> It needs to know which element inside the array you want
to place in the
> textfield (or textfiled) :)
>
> If that doesn't work, double check that your instance
name is correct
> inside
> of "word".
> -
Help with a FOR loop and an object array
I need to make a for loop that takes an array of objects that contain the parameters year, type, and model (all ints) and sort by year, then divide the array in all the objects with the same year and sort them by type, then divide the array again into the objects with the same year AND type and sort them by model.
the object a Dress objects, the get methods are get+nameof parameter.
the array is a 1D array called Dresses.
I have made a paralell array to store the value of the parameters and sort that then move the array acording to that sorted array. The problem is in the division of the array.We'll give your request to do (or finish) your homework for you the attention it deserves.
-
Row operation on array with a For Loop
Hi,
I'm working with a large one dimensional column array. I'm trying to do the following
for i=1:size(A)/2
B(i,: ) =A(1+(i-1)*2:i*2)
So, for example if
A=
0
1
2
3
4
5
6
7
then
B=
0 1
2 3
4 5
6 7
My approach was to use an array subset block inside the for loop to select a number of elements but I'm not sure how to transpose the selected elements into a new row to create the new array B. Is there a more straightforward approach?
Thanks,
Mike
Solved!
Go to Solution.try experimenting with the reshape array feeding it your input array and re-sizing it to output a 2d aray. It is much faster than using a For loop becuase the data never gets moved and LV just changes the array dimensions and updates the stride.
Ben
Ben Rayner
I am currently active on.. MainStream Preppers
Rayner's Ridge is under construction -
Array building with a for loop
I am using indexing on the right edge of a For Loop to try to build a larger 2-D array out of smaller 2-D arrays but the output of the tunnel insists on being a 3-D array. I will attach my VI. The sub VIs are not important; the comments in the block diagram should explain the situation sufficiently. Thanks!
Attachments:
Loop_array_example.vi 24 KBPlease disregard this request for help. I solved my problem by using 'insert into array' instead of building array.
-
Hello,
I hope someone can point me in the right direction for my next assignment. I am very new at Java programming. For my next assignment for class I will need expand my previous program which was a mortgage calculator. The first assignment we wrote a program to show the monthly payments of a $200,000 loan at 5.75% for 30 years. Now we have to output all 360 monthly payments with pauses in between so it doesnt scoll off the page. The calculation that we use is:
month_payments = (principle * monthlyinterest) / (1-Math.pow(1 + monthlyinterest, - months));
My question is how do I get the correct calculation outputed 360 times. I would like to use the "for loop" to do this. Also what is the correct way to do the pause command? I do not want someone to write the code, I just want some help to where I need to start off, I want to learn this on my own.
Thanks in advance,
DCfor (int i ; i < 10 ; i ++ ) { System.out.println("Come sail away"); }
or;
int i = 0;
while(i < 10)
i++;
System.out.println("Come sail away");
} -
Need help with basic "for" loops!
Here is my prompt for class:
Write a program that prompts the user to enter a sentence from the keyboard using JOptionPane.showInputDialog.
The program will print the characters back with the first letter of each word changed from lower case into upper case. If you have a capital letter in the original line and it is not the first letter of a word, then this letter should be switched from upper case to lower case. The only capital letters that should appear in the line must be the beginning letter of every word in the line. All other characters will remain the same.
I figured everything out except for one part. How do I make the first letter of each word change from lower case into uppercase? How do I switch a letter that is uppercase in the middle of a word to lowercase? Last but not least, how do I make sure that the only capital letters in the sentence are the first letter of each word?
I need to do this using for Loops, charAt(), and if/else statements because this is just an intro class. I just can't figure this last part out! Help please!String words = ...;
StringBuilder builder = new StringBuilder();
for (int i = 0; i < words.length; i++) {
char c = words.charAt(i);
boolean isUpper = Character.?(look up the methods in java.lang.Character)
boolean isLetter = ? (there are actually 2 ways to do this. Hint: you can use <= and >= and && to solve this)
if (!isLetter) {
continue;
} else {
// check if this is the start of the word. How do you know that you are at the start of a word?
if (!isUpper && isStartOfTheWord) c = Character.toUpperCase(c); //Hint: there is a way you can do this with x + y
else if (isUpper) //make c lower case
builder.append(c);
return builder.toString(); -
PROBLEMS With my FOR LOOP , PLEASE HELP!!!
Hello,
I have the following event action button method. It has a loop itterates 10 times. I have an imagePanel of a map with an image of a car over the map. The car image moves with the method moveCar();
MY PROBLEM: even though i have my moveCar() method inside the for loop, it doesnt move the car on the map until the loop is finished executing and the car image just jumps over. Can somebody pleae tell me why it does this and how to fix it?????????? thanks so much public ActionListener startActionListener() {
ActionListener listener = new ActionListener() {
public void actionPerformed(ActionEvent event) {
for (int i=0; i<10; i++) {
imagePanel.moveCar();
try {
Thread.sleep(1000); //waits approx. 1 second
} catch (InterruptedException e) {
System.exit(0);
return listener;
}thanks!Well, the easiest way is like this:
public ActionListener startActionListener()
ActionListener listener = new ActionListener()
public void actionPerformed(ActionEvent event)
new Thread(new Runnable()
public void run()
for (int i=0; i<10; i++)
imagePanel.moveCar();
imagePanel.repaint();
try
Thread.sleep(1000); //waits approx. 1 second
catch (InterruptedException e)
//System.exit(0);
}).start();
return listener;
}I simply converted your loop to run asynchronously and added a call to repaint after the move. You would be better off to use a Swing Timer instead, but I will leave that to you. -
How to insert elements into an array after each iteration of a for loop
I am new to labview and working on an application where I am supposed to store an element into an array (without overwriting) after each iteration in a for loop. I have tried using Build Array Function keeping the indicator outside the for loop and played with indexing but didn't work. Please suggest me an idea how to do it.
Thanks
Solved!
Go to Solution.Thank you for your suggestion.Here is my actual application attached . In the first image, a difference in time is evaluated and an enum const of insert into array is passed to the shift register where it takes to Insert element into array phase (Second image). I need to enter the time difference into an array after every loop iteration. Please have a look and could you let me know where I am mislead.
Attachments:
Image 1.JPG 88 KB
Image 2.JPG 71 KB
Maybe you are looking for
-
IPad Dock (latest version) + Digital AV Adapter = Works?
Hey guys, Anyone know if the latest iPad dock works with that newer style Digital AV Adaptor and if the audio out still works?? it didnt on the original iPad 1 dock and a iPad 2 but I now have an ipad 3 so will get a new dock if it will work!! Cheers
-
Problem Bug With My Ipod 30Gb Black (5.5g)
Hello everybody j'ai un ipod 30gb black (5.5gb) et j'ai un petit problem pour les vidéos j'ai a peux pres 40 clips vidéo donc je lance une pour regarder et apres si je mets une autre ou encore une autre ca bloque et je suis obliger de cliquer sur Cen
-
I want to transfer my data that I have in Quicken on a pc to my new apple macbook pro. Use Quiken for bill pay and track investments. Has anyone had any experience with that and what would you recommend? Thanks
-
Hi, I have do load data from Oracle and SQL Server and transform it with OWB. Does anyone know which licenses I must have. Must I download OWB or can I use den OWB included in the database? Oracle 11g and Linux. Regards Siegwin
-
Converting to a .sh for DeployStudio
Hi Guys, I wrote something simple with applescript but it doesn't seem to work at all with the application DeployStudio. Is there anyway to convert this to a .sh? I was trying to see if diskutil would be able to detect volume identifiers but with no