Reputation: 37
Code below counts files with certain name. TypeCount is then some number (four for example).
File dir = new File(Environment.getExternalStorageDirectory().toString(), "/AppDir/" );
File[] files=dir.listFiles();
int typeCount = 0;
String type = "dog";
for (int i=0; i<files.length; i++) {
File file = files[i];
String filepath = file.getPath();
if(filepath.contains(type)){
typeCount = typeCount + 1;
}
}
In this code I want to put every path (File) in the List<File>
. But when I set typeCount to size of the List I get always zero instead.
File dir = new File(Environment.getExternalStorageDirectory().toString(), "/AppDir/" );
File dir = new File(Environment.getExternalStorageDirectory().toString(), "/AppDir/" );
File[] files=dir.listFiles();
int typeCount = 0;
String typeype = "dog";
List<File> myList;
myList = new List<File>() {
@Override
public void add(int i, File file) {
}
@Override
public boolean add(File file) {
return false;
}
@Override
public boolean addAll(int i, Collection<? extends File> collection) {
return false;
}
@Override
public boolean addAll(Collection<? extends File> collection) {
return false;
}
@Override
public void clear() {
}
@Override
public boolean contains(Object o) {
return false;
}
@Override
public boolean containsAll(Collection<?> collection) {
return false;
}
@Override
public File get(int i) {
return null;
}
@Override
public int indexOf(Object o) {
return 0;
}
@Override
public boolean isEmpty() {
return false;
}
@NonNull
@Override
public Iterator<File> iterator() {
return null;
}
@Override
public int lastIndexOf(Object o) {
return 0;
}
@Override
public ListIterator<File> listIterator() {
return null;
}
@NonNull
@Override
public ListIterator<File> listIterator(int i) {
return null;
}
@Override
public File remove(int i) {
return null;
}
@Override
public boolean remove(Object o) {
return false;
}
@Override
public boolean removeAll(Collection<?> collection) {
return false;
}
@Override
public boolean retainAll(Collection<?> collection) {
return false;
}
@Override
public File set(int i, File file) {
return null;
}
@Override
public int size() {
return 0;
}
@NonNull
@Override
public List<File> subList(int i, int i1) {
return null;
}
@NonNull
@Override
public Object[] toArray() {
return new Object[0];
}
@NonNull
@Override
public <T> T[] toArray(T[] ts) {
return null;
}
};
for (int i=0; i<files.length; i++){
File file = files[i];
String filepath = file.getPath();
if(filepath.contains(type)){
myList.add(file);
}
}
typeCount = myList.size();
What is wrong here?
(And a little off topic - Is path written correctly? I'm not sure about it.)
Upvotes: 0
Views: 60
Reputation: 11978
This method size()
will always print 0
because your own List
implementation has a wrong returning statement:
@Override
public int size() {
return 0; // Oops!
}
Another thing too, you don't really insert anything in your List
because of this:
@Override
public boolean add(File file) {
return false; // Hum...
}
Your methods aren't completed yet to execute the same tasks as a normal List
. You better should use ArrayList<File>
or List<File>
which will have all the right methods and won't require hard work from you. Don't reinvent the wheel ;)
Finally, the path are right declared, but you should test if the files are presents in the folder before executing the code. Something as follows:
File[] files = dir.listFiles();
if (files.length > 0) {
// loop and add to a list
}
Upvotes: 1