Automatic adding of elements to an array

2019-05-30 01:41发布

Hi I'm not particular good at Java so please bear with me. I'm trying to write a very simple android app now and I need help with some coding.

Thing is, I have a server that automatically generates .png files and saves them to a public directory in a numerical order. The update occurs daily and is non-exhaustive.

Is there anyway in which I can assign the dynamic values to an array within my app?

            private String[] myRemoteImages = {
            "http://hypotheticalurl1.png",
            "http://hypotheticalurl2.png",
            "http://hypotheticalurl3.png",
            "http://hypotheticalurl4.png",
            "http://hypotheticalurl5.png",
            "http://hypotheticalurl6.png",
            "http://hypotheticalurl7.png",
            "http://hypotheticalurl8.png",
            "http://hypotheticalurl9.png",
            "http://hypotheticalurl10.png",
            "http://hypotheticalurl11.png",
            "http://hypotheticalurl12.png",
            //...blah blah blah
            // these are all dynamically created so I won't know what is the last number on the list
    };

This array will eventually be used to get the images from my server using the app. It works so far but that's only with hardcoded URLs. I would like the URLs to be dynamic, as the number of images will change from day to day.

I'm doubting that regex will work well in Java but then again I'm no expert. Was thinking of perhaps writing a script on the server end that generates a list of existing values and somehow parsing that with the android app.

Can anyone point me in the right direction? Thanks in advance.

Clarification:

The array doesn't have to be dynamically sized while the app is running.

I need a way to read the list of existing images in a remote directory and pass that information to populate the array automatically at runtime.

Resolved

Guys, thanks for the help. Sorry if I wasn't clear enough.

I've found a way to do it. Basically it was rather simple, which was to append an extra line of code to the shell script on the server end to generate a text list of existent image URLs at the same time that it generates the images.

After that, I used a combination of BufferedReader and openStream on the app to parse the remote text file into a String array.

6条回答
Evening l夕情丶
2楼-- · 2019-05-30 01:43

I'd use an ArrayList in this case. You don't have to know the number of elements you want to add then and it's very simple to append elements at the end.

private List<String> list = new ArrayList<String>();

Then simply add elements by

list.add("http://hypotheticalurl1.png");

Regards, Patrick

查看更多
Fickle 薄情
3楼-- · 2019-05-30 01:43

According to your scenario you need to have the followings:

1- a Web Service which has a method to get you the list of the available image names.

2- You need a web service client for your android application, I suggest you to use KSOAP 2 because it is widely known and easy to implement. (If you can't figure out how to use the ksoap in your program, I can provide you some example codes)

3- You need to use ArrayList(java.util) to hold your dynamically sized array.

查看更多
该账号已被封号
4楼-- · 2019-05-30 01:50

With an array you can :

  • change the elements of the array

but you can't :

  • add or remove elements. The number of elements if fixed in an array. Some workaround can be found like putting null values and discarding theem when using the values in the array. But that's more troublesome than really useful.

On the other hand, if you want a full dynamic "array" : use a list (java.util.List). An ArrayList would be interesting here, or even a Vector as you will probably need some multihtreading around this array. With a list you can add and remove elements, size can vary and elements can be replaced.

查看更多
Rolldiameter
5楼-- · 2019-05-30 01:59

instead of using Array of String use ArrayList<String> It will gives you more flexibility on adding and removing item on runtime refer this link...http://docs.oracle.com/javase/1.4.2/docs/api/java/util/ArrayList.html

hear you can find example on arraylist...http://www.java2s.com/Tutorial/Java/0140_Collections/0160_ArrayList.htm

hope that helps

查看更多
Emotional °昔
6楼-- · 2019-05-30 01:59

Hey ytou can do it via
ArrayList stringList = new ArrayList();

stringList.add("Item");
查看更多
祖国的老花朵
7楼-- · 2019-05-30 02:08

thanks for the help. Sorry if I wasn't clear enough.

I've found a way to do it. Basically it was rather simple, which was to append an extra line of code to the shell script on the server end to generate a text list of existent image URLs at the same time that it generates the images.

After that, I used a combination of BufferedReader and openStream on the app to parse the remote text file into a String array.

查看更多
登录 后发表回答