0

I have an ArrayList of an bean class consisting five fields as

Java Bean -- MyShares

  1. fileName
  2. filePath
  3. fileSize
  4. isShared
  5. Creator

I Want to make a ArrayList of filePath from this arraylist of bean class

I don't have vast knowledge on Java collections. So what will be the shortest logic for this.

The present logic which I have implemented is below, Now I want an optimized Logic to do so

ArrayList<Myshares> fileDetails = new ReadSDCard().getSdCardFiles();

        if (!fileDetails.isEmpty()) {

            for (int i = 0; i < fileDetails.size(); i++) {
                CommonUtilities.filePaths.add(fileDetails.get(i).getPath());
            }
        }

        if (!CommonUtilities.filePaths.isEmpty()) {

            for (int i = 0; i < CommonUtilities.filePaths.size(); i++) {

                Log.d(Integer.toString(i), CommonUtilities.filePaths.get(i));
            }
        }

CommonUtilities.filePaths is my static ArrayList in which I want to store the file paths

9
  • What is your question? Commented Apr 11, 2013 at 11:25
  • 1
    Use for-each (for (Myshares share : fileDetails)) instead of the index based iteration. Then you don't need to check if the list is empty. Commented Apr 11, 2013 at 11:29
  • @user714965 is there any difference in speed pf execution in using for each and indexed for Commented Apr 11, 2013 at 11:30
  • Don't know, maybe.. try it. The logic will be optimized though (regarding readability). Commented Apr 11, 2013 at 11:40
  • @user714965 I am trying waitb 1 min Commented Apr 11, 2013 at 11:41

3 Answers 3

1

What you doing is fine but u need to check whether path is null before inserting in CommonUtilities.filePaths. You can also use a iterator instead of for loop.

You can may be avoid two for loops and just do the following:

ArrayList<Myshares> fileDetails = new ReadSDCard().getSdCardFiles();
       int j=0;

        if (!fileDetails.isEmpty()) {
            for (int i = 0; i < fileDetails.size(); i++) {
              if(fileDetails.get(i).getPath()!=null){
                CommonUtilities.filePaths.add(fileDetails.get(i).getPath());
                Log.d(Integer.toString(j), CommonUtilities.filePaths.get(j));
                j++;
              }
            }
        }
Sign up to request clarification or add additional context in comments.

5 Comments

@Nikhil i just suggested checking null because would you really want to add a null because 'CommonUtilities.filePaths' will contain null values and if your trying to use the elements of this list elsewhere you will get NullPointerException so it is better to check if the data is null before insertion as ArrayList can contain null values. Plus 'Log.d(Integer.toString(j), CommonUtilities.filePaths.get(j));' will log null values if you are not checking for null.
Y that is true But for that purpose I have created a check null function which detects for data type and then check for null and if null assign a genuine value to that. And here I have asked my problem and someone has down-voted me. People has to specify reasons why they are downvoting
@Nikhil good if you are checking for null i just gave an answer to your question on how this code can be improved from what i can see here there was no function to check null, plus i had also suggested the use of single loop and i didn't down vote you.
No I am not saying you have down voted me just I am discussing the issue with you. And thanks for the suggestion. Can I have your contact details.
0

Im struggeling to understand why you would have two loops. One that loops through the fileDetails collection and add the paths to a new collection, and then you loop through the new collection for logging. Do these two steps in the same loop:

    if (!fileDetails.isEmpty()) {

        for (int i = 0; i < fileDetails.size(); i++) {
            CommonUtilities.filePaths.add(fileDetails.get(i).getPath());
            Log.d(Integer.toString(i), fileDetails.get(i).getPath());
        }
    }

Other then that I cannot see how this could be optimated since it's impossible to add the paths to a new array without looping through the one with fileDetails. This logic will run in O(n) time

5 Comments

You should cache fileDetails.get(i).getPath() in a local variable to avoid calling it twice.
@John Snow I have used two for loops for some purpose. I have to write some code over there. By any method Can't we extract that one field value means the file path in single line of code without for loop
Well, that kind of fine tuning is not really worth it. A call to a getter would be no different in having the garbage collector handeling the extra local variable. It also requires one more lines of code for no perfomance gain
@Nikhil - If you want to access a known index, sure that could be done. But if you want to add a path for each object in your collection you have to loop through it
Actually I would spend two lines of code. One for Object detail = fileDetails.get(i) and one for String path = detail.getPath(). This makes it easier to find null pointers from the log file for example and makes it easier to maintain also. Trust me there are enough free lines available you don't have to spare them.
0

Why to use CommonUtilities again to get the file paths ? We can log the path at the same instance while iterating

 ArrayList<Myshares> fileDetails = new ReadSDCard().getSdCardFiles();

            if (!fileDetails.isEmpty()) {

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

                    CommonUtilities.filePaths.add(fileDetails.get(i).getPath());
                Log.d(Integer.toString(i),fileDetails.get(i).getPath());
                }
            }

Please see the construct :

  for (int i = 0,n=fileDetails.size(); i < n; i++)

We can use this , provided , we are sure that fileDetails.size() will return same result (list is not being modified). This will save us from calling fileDetails.size() each time

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.