Need help with creating an array in JAVA with loop
Sorry, I don't know Java yet.
sorry harsha i have not studied java.i will cover it soon and then i will able to tell u.sorry
Ok. No problem. Good luck
srry i cant help
Okay
@Abhisar
Java or Javascript ?
Java
@thomaster
@uri
@e.mccormick
What problem are you having with it?
I want to know how to create an array with the loop
I am not sure what you mean. In the question you said with void and now with loop. Typically it is a data type followed by [] and then the variable name.
Oh sorry. Not with void its loop
I know how to create a array by assigning values separately. But I want to assign values with loop
Ah, that is not creating the array. That is populating it. You create, or declare, the array outside the loop. Then you use the loop to iterate (cycle through) the array. Then you can populate the array inside the loop.
Okay. Can you tell me how?
??
There are two ways to do this: First method uses a for or while loop and a numeric iterator. For example: ``` double[] someNumbers = {3.1415, 2.7182, 1.5708}; for (int i = 0; i < 2; i++) { System.out.println(someNumbers[i]); } ``` The other method uses an iterator variable rather than a counter. Thus is also known as the "for each" loop and is not in older versions of Java. ``` double[] someNumbers = {3.1415, 2.7182, 1.5708}; for (double i: someNumbers) { System.out.println(i); } ``` Notice how this second one does not need to call an element by number. It runs once for every number. This has advantages and disadvantages.
On the advantage side of the for each, it is really quick to write and use. It works with arrays and arraylists. Arraylists are more advanced arrays where you don't always know exactly how many things will be needed. On the other hand, if you have a partially filled array, for each will try to loop through elements that don't exist, have nothing in them, or are filled with garbage. That can cause bad results. Also, you may want to do something like only every other item in an array, so you may want to increment by 2 rather than 1. Or, you may want to start at the top and count down, or work on elements 5 to 9, but not the rest. In all those cases, for each is a problem.
Oh okay
Now, I showed printing. You can just as easily do some sort of assignment, a method call, etc.
Ok. That was very helpful. Thank you. Here is an example with me could you explain?
There are also a ton of methods used with arrays: http://docs.oracle.com/javase/7/docs/api/java/util/Arrays.html sort and length are two you see a lot.
I took the below code from here: http://stackoverflow.com/questions/19456509/how-to-create-populate-and-print-a-string-array-in-java ``` import java.util.Scanner; public class TestArray { public static void main(String[] args){ Scanner input = new Scanner(System.in); System.out.println("Enter a number between 1 and 5"); int number = input.nextInt(); System.out.println("Now enter " + number + " names"); //NOTE: You may want to remove this line since it will mess up your data. //String names = input.next(); String[] nameList = new String[number]; for(int i = 0; i < number; i++){ nameList[i] = input.next(); } //print separate here. System.out.println("Here are the names you entered: "); for(int i = 0; i<number; i++){ System.out.println(nameList[i]); } } } ``` As you can see, it asks a person for a number of names. Then it makes an array that is that size. In the first loop it populates the array with that number of names. In the second array it prints them back out.
class TwoDArray { public static void main(String args[]) { int TwoD[][] = new int [4][5]; int i, j , k = 0; for(i = 0; i < 4; i++) for(j = 0; j < 5; j++) { twoD[i][j] = k; k++; } for(i = 0; i < 4; i++) for(j = 0; j < 5; j++) System.out.print(twoD[i][j] + " "); System.out.println(); } } }
OK. Same basic concept. Just progressively puts things into the array.
I didn't understand here : twoD[i][j] = k; k++
That is the integer that is being put into the array.
One problem I do see is your array is naed TwoD but what you use is twoD.
Are you trying to print all the results on one line or in a block?
block
Oh yeah. I have to use a capital there
Your `System.out.println();` is in the wrong place to do a block. It is outside the loop. Outside the loop: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Inside the outter loop: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 Inside the inner loop: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
Thanks :)
Learning "Computer Architecture and Organisation" useful for better understanding of JAVA?
Computer Architecture and Organisation is better for understanding computers. It heps programmers because when you know a computer at the lower levels you get a better idea of what it will do.
Okay. So do you know any books relating to it?
``` /* * This is a changed version of the * twoD multi-dimensional array example */ class TwoDArray { public static void main(String args[]) { // Note that final (unchangeable) values set // This makes editing easer because these are used // in the loops. So making more or less rows or // columns is very simple. final int ROWS = 4; final int COLS = 5; int twoD[][] = new int [ROWS][COLS]; // Rather than calling everything i, j, and k, a // variable should have meaning. In this case, // I changed k to magicNumebr because it is what // we are saving in the array. It mist be special // for some reason! So it is magic! int magicNumebr = 0; // Notice that I moved the declaration of i and j // to in the loops. They exists only for these loops, // so it is cleaner to declare them in the loops. // Then they go away when the loops do. for(int i = 0; i < ROWS; i++) for(int j = 0; j < COLS; j++) twoD[i][j] = magicNumebr++; // Here, I used k just because. It does not have to be // i and j. But I reused j. Reuse like this is fine // because of how it is going away with the loop. // Also take note of the output formatting. for(int k = 0; k < ROWS; k++) { for(int j = 0; j < COLS; j++) System.out.format("%4d", twoD[k][j] ); System.out.println(); } } } ```
Try these sites: http://www.onlineprogrammingbooks.com/ http://freecomputerbooks.com/ http://www.freetechbooks.com/ Another name for that topic is "machine organization."
Join our real-time social learning platform and learn together with your friends!