Difference between String and String[] Code Answer

Hello Developer, Hope you guys are doing great. Today at Tutorial Guruji Official website, we are sharing the answer of Difference between String and String[] without wasting too much if your time.

The question is published on by Tutorial Guruji team.

I am trying to add the strings to a csv file in Android. As per the syntax it is asking to add String [] but I have added below line

String [] s1;    

retrieving the value from cursot and storing it in s1. Above line giving me the error:

Type mismatch: cannot convert from String to String[]

What is the difference between String and String[], and how can I convert to String[]?

I am a beginner to Android and Java.


I was able to store but unable to store in writeline of csv class.

CSVWriter export=new CSVWriter(new FileWriter("/sdcard/"+stock+".csv"));
export.writeAll(s1, true);


The method writeAll(List<String[]>, boolean) in the type
CSVWriter is not applicable for the arguments (String[], boolean)


String is a single String object String[] is an array of String objects. The problem is you are trying to add a String Object to a String array without specifying an index.

You could do something like:

String [] s1 = new String[1];    

Just an example or you could just create a String Object instead of a String array:

String s1;    

Basically if you want to use an array you must specify an index of that array to Store the object because there are multiple objects stored in the array each one has its own index.

Try doing this:

ArrayList<String[]> csvExport = new ArrayList<String[]>();
CSVWriter export=new CSVWriter(new FileWriter("/sdcard/"+stock+".csv"));
export.writeAll(csvExport, true);

This will only add one line to your csv file. If you want multiple lines you will need to create multiple String[] and add each String[] to your ArrayList csvExport.

Think of a String[] array as your columns for example:

String[] columnNames = new String[2];

columnNames[0] = "ID";
columnNames[1] = "Name";

String[] person1 = new String[2];

person1[0] = "1";
person1[1] = "George";

ArrayList<String[]> csvExport = new ArrayList<String[]>();
CSVWriter export=new CSVWriter(new FileWriter("/sdcard/"+stock+".csv"));
export.writeAll(csvExport, true);

The code above would give you a csv file like this:

ID Name

1 George

We are here to answer your question about Difference between String and String[] - If you find the proper solution, please don't forgot to share this with your team members.

Related Posts

Tutorial Guruji