Re: return array of strings to class
sazykin@gmail.com wrote:
Hi,
i'm wriritng a smal programm and i whant to return array of string to
class here is an example i whant to do...
public class CreateArray{
public static String[] create_str_array(){
//here i'm reading a txt file to string array..
StringArray[i] = dis.readLine(); i++;
then i'm trying to return my array with
return (String[])(line)
}}
ok no i supposed to have array of strings and i would like to access it
in my new class
public class AccessArray{
public static accessArray(String[] StringArray) {
//and here comes that i dont know how can access my array....
I have tried:
CreateArray[] StringArray = new CreateArray[20];
for (int i=0; i>10;i++)
String newArray[i] = line[i].create_str_array();
//but this doesnt seems to work. please help....
}}
i whant to have pass exactly the same array to this class from my
CreateArray class
thanx
A method to return an array of Strings:
String[] method() {
String[] strArray = {"one","two","three"};
return strArray;
}
When you create an array of Objects you need to create the array
reference and then you need to populate the array.
String str = "three";
String[] strArray = new String[3];
strArray[0] = new String("one");
strArray[1] = "two";
strArray[2] = str;
If you are going to read Strings from a text file, the easiest way to
get them into a String[] is to read and add them to an ArrayList. When
you are done reading, create a String[] from the ArrayList.
FileInputStream fis = new FileInputStream("file.txt");
InputStreamReader isr = new InputStreamReader(fis);
BufferedReader br = new BufferedReader(isr);
String str = null;
ArrayList<String> list = new ArrayList<String>();
while ((str = br.readLine()) != null)
list.add(str);
int length = list.size();
String[] strArray = new String[length];
strArray = list.toArray(strArray);
You will need to add your own Exception handling code to the above.
--
Knute Johnson
email s/nospam/knute/