How do you:
1. Initialize (create) an Array.
2. Push a String value into it.
3. Push another String value into it.
4. Dump it to get its contents.
-
1This pretty much sounds like homework. In the future you should be tagging as such :) Else answers like "Use ArrayList", "Use Stack", etc would be after all utterly pointless.BalusC– BalusC2010-06-15 18:27:06 +00:00Commented Jun 15, 2010 at 18:27
-
Not a homework. I apologize if the question seems inappropriate.Emanuil Rusev– Emanuil Rusev2010-06-15 18:30:02 +00:00Commented Jun 15, 2010 at 18:30
-
if it isn't homework then you should use the Collections classes, List, Set, Map, etc. Direct use of Array is a code smell 99.99999% of the time.user177800– user1778002010-06-15 18:33:48 +00:00Commented Jun 15, 2010 at 18:33
-
I'm new to Java. My background is in ActionScript. In ActionScript these types of objects you mention don't exist. Arrays do.Emanuil Rusev– Emanuil Rusev2010-06-15 18:36:39 +00:00Commented Jun 15, 2010 at 18:36
-
3OK, nevermind then. I suspected homework because the tutorial is pretty easy to find at sun.com and the average student doesn't research at all ;)BalusC– BalusC2010-06-15 18:40:48 +00:00Commented Jun 15, 2010 at 18:40
6 Answers
Arrays in Java are a fixed size, determined when you create them. As such, they have no push methods.
It sounds like you want a List instead, most likely an ArrayList<String>. Lists have an add function for adding new elements.
The Java Collections trail has more information about the various types of collections (List, Set, and Map).
Lists and Sets work with Java's for each operator:
List<String> myList = new ArrayList<String>();
//List<String> myList = new LinkedList<String>();
myList.add("One");
myList.add("Two");
// Because we're using a Generic collection, the compiler
// inserts a cast on the next line for you
for (String current : myList) {
// This section happens once for each elements in myList
System.out.println(current);
}
// should print "One" and "Two" (without quotes) on separate lines
2 Comments
int[] a;
a = new int[5];
a[0]=1;
a[1]=2;
a[2]=3;
a[3]=4;
a[4]=5;
for(int i =0; i<5; i++)
System.out.println(a[i]);
Java.sun has a good link for array help: http://java.sun.com/docs/books/tutorial/java/nutsandbolts/arrays.html This is basically a fixed size array. If you are looking to push elements in (you do not know the size) you'll want to look at an ArrayList.
Comments
Basically an array is a way to hold more than one value at a time. It's like a list of items.In java, initialise an array can be done by using new keyword for example,
int arrayName = new int[10];
Assigning values to arrays:
arrayName[0] = 10;
also,
int[]ArrList = {1, 2, 3, 4,5};
Simple example for int array java are as follows,
public class array_ex {
public static void main(String []args) {
int arrex[] = {10,20,30}; //Declaring and initializing an array of three elements
for (int i=0;i<arrex.length;i++){
System.out.println(arrex[i]);
}
}
}
this is also an another example for declaring array,
public class array_ex {
public static void main(String []args) {
int arrex[] = new int[3]; //declaring array of three items
arrex[0] =10;
arrex[1] =20;
arrex[2] =30;
//Printing array
for (int i=0;i<arrex.length;i++){
System.out.println(arrex[i]);
}
}
}
example for pushing s string value
ArrayList<String> ar = new ArrayList<String>();
String s1 ="sub1";
String s2 ="sub2";
String s3 ="sub3";
ar.add(s1);
ar.add(s2);
ar.add(s3);
String s4 ="sub4";
ar.add(s4);
hope it is helpful!