Java String formation for ListView - java

I have created a Java code for my Android App.
String[] MovieName=new String[]{};
for (int i = 0; i < 15; i++)
{
MovieName[i]=movieAtt.getAttributeValue( "name" ); //Value coming from my XML
}
ListViewObject.setAdapter(new ArrayAdapter<String>(screen2.this,android.R.layout.simple_list_item_1 , MovieName));
This code throws an Exception.
I think i am not inserting vaues properly inside Java String Array.
All i want is to have a variable like MovieName={"1","2", "3"} to feed into the ListView of my code.
This is not much helpful too :
http://download.oracle.com/javase/6/docs/api/java/lang/String.html

You initialize an empty array.
Try this
String[] MovieName = new String[15];

Your initilizing an empty string array. That will give you an ArrayOutOfBoundException.
If you always have 15 entries you could initialize it to 15.
String[] MovieName=new String[15];
Otherwise you could create an ArrayList and convert it to an array after you filled it.

If number of elements in MovieName is constant, then you should initialise it as
String[] MovieName=new String[15];
Your current initialisation is equal to
String[] MovieName=new String[0];

Related

java: does the string .split method change the size of an array

So I initialized an array lets say
`string example = new string [5];
When I called the split method on a line of
string x = "abc, def, g";
example = x.split(",");
example[0] = abc
example[1] = def
example[2] = g
I can no longer access example[3] and example[4] as I am getting null pointer
shouldn't these still be accessible with values of null?
Even though you created a array with some initial length for example
String [] sample = new String[5];
after assigning new Array to the variable It will create a new array with new array size. for example.
String s = "Hi how are you";
sample = s.split(" ");
so you can not access old array elements.
It doesnt preserve the values. "example" is assigned with a completely new and different array.
If you want to preserve the number of elements previously present in the array, you can do something like:
int num = example.length;
example = x.split(",");
example = Arrays.copyOf(example, num);
// initialize the new array elements here.
Of course doing so is not very efficent and should be avoided. I suggest you take a look at array lists instead.
Your initial example array (of length 5 with null values) gets overwritten by the new array returned by the call to the split() method, in this case an array of length 3. The initial array referenced by the example reference is not accessible anymore, it will be garbage collected.
If you want to keep both arrays you can assign the result of the split to another variable
String[] example2 = x.split(",");

Array index out of bond by putting Array Zero index value to another array?

I am not understanding may be its some thing silly mistake in it
i have array
String[] Data
Below string is at zero index of Data,
[ ["Walls", "Floors", "Ceilings", "Pillars", , "];
and i am willing to put it in loop in some new array at zero index like
String[] Description = null;
Description[0]=Data[0]
It gives error Array index out of bond??
what mistake is going on ?
i wanna put above string at index 0 of Description
hopes for your suggestion
DETAIL:
String[] Description=null;
String Manupulate ="";
for(int ques=0;ques<MultiQuestion.length;ques++)
{
Manupulate = "["Walls", "Floors", "Ceilings", "Pillars"], "";
String[] Detail = Manupulate.split("]");
Description[ques] = Detail[0];
}
Detail[0] will get ["Walls", "Floors", "Ceilings", "Pillars"]
I want to get Detail[0] value in Description[0].
I am using i will have to get Detail value in Descriotion[1] and so on till loop ends
Hopes for your suggestion
If you don't initialize Description, it should give you NullPointerException, not ArrayIndexOutOfBoundsException.
Try
String[] Description = new String[Data.length];
Description[0]=Data[0];
You have not defined length for description.You need to allocate that array memory by :
Description = new String[ARRAY_SIZE];

adding elements from list to string array

I have a String array like,
String[] abc= new String[]{};
and my List has some values. I iterate the list and add each list element to string array.
for(int i=0; i<errList.size(); i++)
{
abc[i] = errList.get(i).getSrceCd();
}
errList.size() has 6 values. But when the for loops executed I get java.lang.ArrayIndexOutOfBoundsException. Any inputs?
You're creating a String[] object of zero length; so, when you try to assign an item to abc[i], it is accessing an index not within your bounds 0 <= i < 0.
You should allocate abc with a length instead:
String[] abc= new String[errList.size()];
for(int i=0; i<errList.size(); i++)
{
abc[i] = errList.get(i).getSrceCd();
}
You need to craete the string array with the same size as the list. It is not dynamic. Perhaps you can tell what you are trying to achieve with this exercise.
Did you try to use for each loop which is widely used in collection framework?
String[] abc = errList.toArray(new String[0]);
Or:
String[] abc = new String[errList.size()];
errList.toArray(abc);
I would just do this
String[] abc= errList.toArray(new String[errList.size()]);

Java - Creating an Undefined Array

I'm looking to create an array that will be able to change size over time because the size of the array is unpredictable and I don't want to create a huge random number that will waste memory so every time a button is pressed I need the array to grow by one.
private String[][] lyricLineInfo = new String[x][5];
In the place of x is where the array must grow upon the button push and 5 is a constant. So I need the x button to grow by one without overflowing. Can I do it by using something like this?
lyricLineInfo[lyricLineInfo.length + 1][4] = fieldLyrics.getText();
Anyways thanks in advance!
Use an ArrayList<String[]> (see the docs here). It will grow automatically. (It uses an internal array that doesn't actually grow by just 1 when it needs to grow. Since growing is an expensive operation, it grows by some larger amount so it can absorb a few more items before having to grow again.)
EDIT
For example, here's how you could recode the two lines of your original post:
private ArrayList<String[]> lyricLineInfo = new ArrayList<String[]>();
lyricLineInfo.add(fieldLyrics.getText());
The second line assumes that fieldLyrics.getText() returns a String[]. If I misunderstood your intent and it returns a String, then you could do the following:
String[] nextStrings = new String[5];
nextStrings[4] = fieldLyrics.getText();
lyricLineInfo.add(nextStrings);
If the second index isn't always 5 long, you can also have an ArrayList of ArrayLists:
private ArrayList<ArrayList<String>> lyricLineInfo
= new ArrayList<ArrayList<String>>();
Then you could lyricLineInfo.add(new ArrayList<String>()); to extend the array.
EDIT 2
#clankfan1 - In your comment, you asked how to do a particular operation. Let's say we're using the ArrayList<ArrayList<String>> structure. It would go something like this:
ArrayList<ArrayList<String>> lyricLineInfo = new ArrayList<ArrayList<String>>();
ArrayList<String> line = new ArrayList<String>();
line.add("true");
line.add("true");
line.add("0.0");
line.add("5.0");
line.add("First Line");
lyricLineInfo.add(line);
line = new ArrayList<String>(); // don't use clear(): need a new object here
line.add("false");
line.add("false");
line.add("5.0");
line.add("10.0");
line.add("Second Line");
lyricLineInfo.add(line);
String secondLineTitle = lyricLineInfo.get(1).get(4); // will be "Second Line"
Obviously, this logic is amenable to being put into a separate method.
EDIT 3
If you need the elements of lyricLineInfo to be of type String[], it is vital that each element be a distinct array. Here are a few coding styles for adding elements:
ArrayList<String[]> lyricLineInfo = new ArrayList<String[]>();
String[] line = { "true", "true", "0.0", "5.0", "First Line" };
lyricLineInfo.add(line);
// now for a second style:
line = new String[5];
line[0] = "false";
line[1] = "false";
line[2] = "5.0";
line[3] = "10.0";
line[4] = "Second Line";
lyricLineInfo.add(line);
// and a third style:
lyricLineInfo.add(new String[] {
"false", "true", "10.0", "15.0", "Third Line"
});
String secondLineTitle = lyricLineInfo.get(1)[4]; // will be "Second Line"
You could use java.util.Vector<String[]>.
Use an ArrayList like this:
private ArrayList<ArrayList<String>> lyricLineInfo = new ArrayList<ArrayList<String>>();
ArrayLists are flexible arrays in java. When you want to add something do this:
lyricLineInfo.add(stringToBeAdded, index) //for the first dimension and
lyricLineInfo.get(firstIndex).add(stringToBeAdded, index); //for the second dimension
Use a List instead:
private List<String[]> lyricLineInfo = new ArrayList<String[]>();
Then to add to the list you use:
lyricLineInfo.add(new String[5]);
and to get you do:
// Get the 3rd element (array index 2).
String[] strings = lyricLineInfo.get(2);
Why don't you think about using Collections if you need an array with undefined size? :)
You can't do that with an array, but you can use a List object. You could try something like this:
private List<String[]> lyricLineInfo = new ArrayList<String[]>();
So then, assuming fieldLyrics.getText() returns a String[], you would do:
lyricLineInfo.add(fieldLyrics.getText());
If you only add new elements and iterate over all elements in list you should use LinkedList instead.
Collections are heavily used in Java. You should check Java Collections Framework - tutorial

Compare arrays and create new array Android SDK

I have two arrays "tags" and "selected" the tags array is static and depending on what the user selects the "select" array values will be set to true or false.
I want to compare the tags array and the select array and the "select" array positions that have true I want to pull out the correspoding position of the "tags" array and bulid a new array.
private String[] tags = new String[] { "Bob", "Tom", "Mike", "Smith" };
private boolean[] selected = new boolean[tags.length];
public String[] selected_tags;
for (int i = 0; i < tags.length; i++) {
if (selected[i] == true){
selected_tags[i] = tags[i];
}
I am not sure if I am doing this correctly because I feel like I would have empty spots in my selected_tag array. If there is a better way of doing this I am open to suggestions.
thanks!
public String[] selected_tags = new String[tags.length]
Everything you say and you did is fine and reasonable. Just make sure all of them are of the same size. (see above)

Categories

Resources