2

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.

5
  • 1
    This 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. Commented Jun 15, 2010 at 18:27
  • Not a homework. I apologize if the question seems inappropriate. Commented 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. Commented 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. Commented Jun 15, 2010 at 18:36
  • 3
    OK, 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 ;) Commented Jun 15, 2010 at 18:40

6 Answers 6

7

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
Sign up to request clarification or add additional context in comments.

2 Comments

I finally got around to adding an example. Hopefully that helps.
Brilliant! Thanks for answering the question I should have asked!
2
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

2

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&lt;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&lt;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!

Comments

1

Why not use a Stack?

final Stack<String> strings = new Stack<String>();
strings.push("First");
strings.push("Second");
System.out.println(strings.toString());

You could also use a List or a Queue depending on your needs.

2 Comments

I wasn't aware that toString on a collection would work, but I guess it makes sense.
Yeah, it prints a nice little list calling .toString() on each element in turn. :D
1

It does sound like you want a list, but in case you really did mean an array...

//1. 
String [] arr = new String[2];
//2.
arr[0] = "first";
//3.
arr[1] = "second";
//4.
for (String s: arr)
{
    System.out.println(s);
}

Comments

1
 anArray = new string[10];
 anArray[0] = "MYSTRING";
 string MyString = anArray[0];

for (int i =0; i <10; i++)
{
   System.out.println(anArray[i]);  
}

Pretty straightforward as far as arrays go, there are a couple of other libraries in java that can help ease the burdens of using raw arrays.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.