0

I come from an action script back ground and i am baffled by how to use arrays in java.

In my main activity i created an empty array called mIcons like so

private Array mIcons;

Now i want to set the value of that array by using a method from my DataBaseHelper class which looks like this:

public Array getHomeScreenIcons() {

        Array iconList;

        // Select All Query
        String selectQuery = "SELECT  * FROM " + homeIcons;

        SQLiteDatabase db = this.getWritableDatabase();
        Cursor cursor = db.rawQuery(selectQuery, null);

        // looping through all rows and adding to list
        if (cursor.moveToFirst()) {
            do {

iconList.push(Integer.parseInt(cursor.getString(0)));

            } while (cursor.moveToNext());
        }
        Log.v(TAG, "List Created"); 
        // return contact list

}

that bold line jumping out of the code is the problem so far.. how do i PUSH to my array

Then later i will want to run a for loop for the array from my main activity using.length

1
  • The best way to learn about the primitive language constructs is through a book or an online tutorial. This question involves such elementary syntax that it doesn't really belong on SO. Commented Mar 7, 2012 at 20:43

4 Answers 4

7

Use an ArrayList paramaterized to any type you want

ArrayList<Integer> iconList = new ArrayList<Integer>();

add with

iconList.add(Integer.parseInt(cursor.getString(0)));

Iterate with

for (int i: iconList)
{
    // i is your entire array starting at index 0
}

or

for (int i=0; i< iconList.size(), i++)
{

}
Sign up to request clarification or add additional context in comments.

4 Comments

This answer is mostly correct, except that you cannot pass primitives as the type of a Collection. You will need ArrayList<Integer> iconList = new ArrayList<Integer>() instead.
Wow, ok guys, first off thank you, secondly, can you explain to me the difference between Array and ArrayList? I was looking it up and apparently an Array has to have a predefined length? Obviously ArrayList is what i want to use, but if someone could explain to me why Array's have to have predefined lengths i would love you.. seems absolutely retarded to me?
@erik They are very similar, see this link: max.cs.kzoo.edu/AP/Workshops/Java/ArraysArrayLists.html . Remember, you cannot use primitives in an ArrayList you have to use type Object
@erik Just think if you need a fixed number of primitives, lets say 10 integers, it will be less resource-intensive to use an Array with int than it would be to use an ArrayList with the Integer object
2

You're probably thinking about ArrayList

private ArrayList<String> iconList = ArrayList<String>();
iconList.add("Stuff");

and then later to loop through

for (int i=0; i<iconList.size(); i++) {
    String newStuff = iconList.get(i);
}

Comments

1

You should probably hit up some basic java tutorials to get used to the array syntax and functionality. http://docs.oracle.com/javase/tutorial/java/nutsandbolts/arrays.html could be a good starting point.

Looking at your specific problem -

You generally don't want to use the Array class in the manner that you do. It's more of a helper class. Also, it seems that you are going for stack semantics, and you'd likely want to use a Stack instead.

First, arrays: you declare an array like so:

Type[] myArray = new Type[arraySize]; 

and then you access it with index like so:

Type myThingFromArray = myArray[myArrayIndex];

and you put things in it like so:

myArray[myTargetIndex] = myObjectOfTypeType;

Raw arrays in java have static size and are not easily growable. For most applications it would be a better idea to use a member of the Collections framework instead. If you're actively looking for a stack (as you mention pushing) then you could use Stack<Integer> and have all the regular stack operations.

Another benefit of using a modern collection class is that you can iterate through your collection using the for-each construct, which eliminates some regular for boilerplate. An example:

public ArrayList<Integer> iconList;
public Array getHomeScreenIcons() {

    Array iconList = new ArrayList<Integer>();

    // Select All Query
    String selectQuery = "SELECT  * FROM " + homeIcons;

    SQLiteDatabase db = this.getWritableDatabase();
    Cursor cursor = db.rawQuery(selectQuery, null);

    // looping through all rows and adding to list
    if (cursor.moveToFirst()) {
        do {
            iconList.add(Integer.parseInt(cursor.getString(0)));
        } while (cursor.moveToNext());
    }
    Log.v(TAG, "List Created"); 
    // return contact list

    //Iterate like so:
    for (Integer i : iconList){ 
        System.out.println("Here's all integers in the icon-list: " + i);
    }
}    

Comments

0

You can define arrays in Java like this:

int[] intArray = new int[3]; // array for three ints
String[] stringArray = new String[10]; // array for 10 Strings

So for your code, you can do something like this:

if (cursor.moveToFirst()) {
    int[] iconList = new int[cursor.getCount()];
    int index = 0;
    do {
        iconList[index] = Integer.parseInt(cursor.getString(0));
        index++;
    } while (cursor.moveToNext());
}

After that you can loop over the array like this:

for (int icon : iconList) {
    // Do something with icon
}

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.