Question:
Question about Java Array?
Eddie
2012-09-07 21:51:55 UTC
Hello All, I'm new to very new Java and just learning the concepts structure of the Java language and need help with Arrays. In reading about Arrays, I've notice that most books do a good job at explaining the format to declare an array but don't do a good job at describing a situation where you would use a Java Arrays. Meaning I understand that Java Arrays can be seen as a cup that hold a value and that's great, I get the concept, but DONT understand where I would use arrays to manipulate/organize data. If anyone can help me to understand or explain a situation where Array's would be used in designing a program. Thank you in advance for your knowledge.
Three answers:
2012-09-07 22:02:02 UTC
You would mostly use them for short list and things like that. However there are much better options for storing list, especially long ones such as ArrayList and other data structures stuff like that. One of the nice things that you can however do with Arrays (and also many of the aforementioned data structures :P) is loop through them. So if you had a short list of values you could print them out quite easily:



String[] names={"dave", "jeorge" ,"matt", "norm"," tommy"};

for(int i=0;i<5;i++)

{

System.out.println(names[i]);

}



Hope that helps :D
husoski
2012-09-07 22:13:15 UTC
Try writing a program that will accept a list of 30 numbers and display them in sorted order, without using arrays (or "enhanced array" type classes like ArrayList).



You can't write a loop that runs through a list of named variables, like val1, val2, val3, ..., val30. But, you can create an int val = new int[30]; array, and loop for i=0 to 29 referring to val[i] each time. Arrays and loops work together to reduce complexity. You can describe a code pattern to run on each array element, and put that pattern in a loop to run it on each entry of an array.
2014-08-31 03:37:22 UTC
complex matter research into google and yahoo that can assist


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Continue reading on narkive:
Loading...