Hi guys,

I am relatively new to java language of which I'm studying at the moment. I Have spent considerable time looking through examples and java class libraries but have not been successful in finding a solution for:
1) naming an Array within a method
2) adding data to the Array of the given name
3) When intialising an array do i need to define a type such String or int e.g. "ArrayList<String> playListName"?
4) If the array is of String type can you later add other types such as int? or is it best to leave as String type and convert other types to String such as if you have a int value as 2011 you would "Integer.toString"

My code thus far

// imports
import java.util.ArrayList;
import java.util.*;

public class Playlist
    // Instance variables
    //public String[] playListName;
    public String playListName = "";
     * Creates a name for the Playlist (type as String)
    public void newPlayListName(String newPlayListName)
      playListName = newPlayListName;
      System.out.println("Current Playlist is " + "{" + newPlayListName + "}");
      ArrayList<String> playListName;
     * Stores Track Reference name
    public void storeTrack(String referenceNumber)

Thanks in advance :)


1) Here is how you name and initialize an array within a method

[I]ArrayType[/I][] [I]ArrayName[/I] = new [I]ArrayType[/I][number of array slots];

Replace arrayType with an: object(like polygon as represented by a class), int, double, long, String, boolean, short, char.

Pick any name you like for the array and put it in place of arrayName.

2) To add data to an array you can use this syntax:

arrayName[slot number] = value;

or you can add the data when you initialize the array like this.

[I]ArrayType[/I][] [I]ArrayName[/I] = {arrayValue1, ArrayValue2, . . ., arrayValueN};

the slots in an array are counted from zero so the first slot is 0 and the last is the total number of slots minus one.

3 + 4) Yes you do have to define the type and the java compiler will complain if you try to put any other type of data in the array. You may add other data types to a string array but you must first convert the data to a string. If you are reading data from the command line simply use a call that returns a string like Scanner.nextLine(); If the data is in your program simply double qoutes around it in order to make it a string. this does not work if the data is stored in a variable then you must use a to string method or implicit casting. Like this: to convert from a int to a double var type:

int interVar = 1;
double decimalValue = (double)integerVar

Tell me if I you don't understand something I will explane it a different way.

hope this helps ( :