Weird "bug" on Java function string seems not be removed off a ArrayDeque instance

Thread Starter

Samantha Groves

Joined Nov 25, 2023
152
I am trying to implement a function
Java:
public void calc(String searchPath,String diskLocation )
which by using the Breadth First Search it finds all files with a relative path of searchPath

I have written down this code:


Java:
    ArrayDeque<String>searchedFiles = new ArrayDeque<>();
    searchedFiles.add(diskLocation);
    while(!searchedFiles.isEmpty())
    {
      for(String i:searchedFiles)
      {
        System.out.println(i);
        File f = new File(i);
        if(f.isFile())
        {
          if(f.getPath()==searchPath)
          {
            foundFiles.add(i);        
          }
        }


          try{
            var files = f.listFiles();
            while(files!=null) {
              for (File fl : files) {
                System.out.println(fl.getCanonicalPath());
              }
            }           
          }
          catch(Exception e)
          {
          }
       searchedFiles.remove(i);
      }
    }
  }
I expected it to output "diskLocation\\" then exit but it continuously prints out the files of the Java File(directory) specified by the diskLocation String and I dont understand why?Am I not removing it off the searchedFiles dequeue?What is happening?
 

ApacheKid

Joined Jan 12, 2015
1,762
I am trying to implement a function
Java:
public void calc(String searchPath,String diskLocation )
which by using the Breadth First Search it finds all files with a relative path of searchPath

I have written down this code:


Java:
    ArrayDeque<String>searchedFiles = new ArrayDeque<>();
    searchedFiles.add(diskLocation);
    while(!searchedFiles.isEmpty())
    {
      for(String i:searchedFiles)
      {
        System.out.println(i);
        File f = new File(i);
        if(f.isFile())
        {
          if(f.getPath()==searchPath)
          {
            foundFiles.add(i);       
          }
        }


          try{
            var files = f.listFiles();
            while(files!=null) {
              for (File fl : files) {
                System.out.println(fl.getCanonicalPath());
              }
            }          
          }
          catch(Exception e)
          {
          }
       searchedFiles.remove(i);
      }
    }
  }
I expected it to output "diskLocation\\" then exit but it continuously prints out the files of the Java File(directory) specified by the diskLocation String and I dont understand why?Am I not removing it off the searchedFiles dequeue?What is happening?
Not an answer (I may have time to analyze that code later) but a question, do you have the option to use C# which is now light years beyond the capabilities of Java, is that an option for you?
 

Thread Starter

Samantha Groves

Joined Nov 25, 2023
152
W
Not an answer (I may have time to analyze that code later) but a question, do you have the option to use C# which is now light years beyond the capabilities of Java, is that an option for you?
I could but why?Many programs are kept on being written for the time being.Java is not outdated by any means.
 

ApacheKid

Joined Jan 12, 2015
1,762
W

I could but why?Many programs are kept on being written for the time being. Java is not outdated by any means.
I'd ask that you consider this at least, perhaps try writing that with C#. You can certainly have a look in your own time at a comparison of these two languages then see if this encourages you to try it.

I use C# routinely and have also used Java in the past, the advantages are considerable especially as C# is now at version 12.

Basically one could likely write what you are trying to write, more easily and more compactly and that will be easier to debug and reason about. The IDE too is streets ahead of Eclipse, the debugging capabilities in Visual Studio are extremely powerful too.

The language has true generics, async support, pattern matching, iterators (async and sync), nullable ref types (enables you to pretty much eliminate null ref exceptions), partial types, raw string literals, tuples and many more things, all of these have been added gradually over the past twenty years, every year a new set of features is added too.

I could help more too of course, as I use C# daily and moved from Java some years ago. I just think the benefits are too much to ignore frankly, it's also not a steep learning curve for any Java programmer.

C# is easily used these days to write portable code for Linux, Windows, iOS, Android etc etc.
 

Thread Starter

Samantha Groves

Joined Nov 25, 2023
152
I'd ask that you consider this at least, perhaps try writing that with C#. You can certainly have a look in your own time at a comparison of these two languages then see if this encourages you to try it.

I use C# routinely and have also used Java in the past, the advantages are considerable especially as C# is now at version 12.

Basically one could likely write what you are trying to write, more easily and more compactly and that will be easier to debug and reason about. The IDE too is streets ahead of Eclipse, the debugging capabilities in Visual Studio are extremely powerful too.

The language has true generics, async support, pattern matching, iterators (async and sync), nullable ref types (enables you to pretty much eliminate null ref exceptions), partial types, raw string literals, tuples and many more things, all of these have been added gradually over the past twenty years, every year a new set of features is added too.

I could help more too of course, as I use C# daily and moved from Java some years ago. I just think the benefits are too much to ignore frankly, it's also not a steep learning curve for any Java programmer.

C# is easily used these days to write portable code for Linux, Windows, iOS, Android etc etc.
For Linux and iOS you need certain extensions since C# is a languange developed by Microsoft.
 

ApacheKid

Joined Jan 12, 2015
1,762
For Linux and iOS you need certain extensions since C# is a language developed by Microsoft.
That's not actually the case, C# is now an internationally standardized language ECMA-344 and nothing special is needed for running code on Linux or iOS, there are huge systems written in C# running on Linux today, the code generated by the C# compiler is 100% decoupled from any platform specifics.

Many Azure and AWS web apps and web services are running on Linux, the code is 100% source or binary portable.

Visual Studio (the MS IDE) is restricted to Windows and Mac (as the IDE is not itself coded wholly in C#) but there are other IDE's that run fine on Linux from other vendors.

The point I want to stress is that C# and .Net (version 8 is current) are 100% platform agnostic, all MS libraries and most 3rd party libraries are agnostic with respect to platform.

As things stand today I'd say there's no compelling reason to be working with Java unless one is working on an existing sizeable code base already or one is tied to Java for some other reason.

Writing code in C# today means writing 100% portable source code for Linux, Windows, Mac, iOS and Android, totally 100% portable no matter how complex that code becomes (these were goals set by MS some years ago when the began the .Net Core initiative - since renamed to simply .Net).

Anyway, your call, I just wanted to take the opportunity to raise this as an option.
 

ApacheKid

Joined Jan 12, 2015
1,762
I am trying to implement a function
Java:
public void calc(String searchPath,String diskLocation )
which by using the Breadth First Search it finds all files with a relative path of searchPath

I have written down this code:


Java:
    ArrayDeque<String>searchedFiles = new ArrayDeque<>();
    searchedFiles.add(diskLocation);
    while(!searchedFiles.isEmpty())
    {
      for(String i:searchedFiles)
      {
        System.out.println(i);
        File f = new File(i);
        if(f.isFile())
        {
          if(f.getPath()==searchPath)
          {
            foundFiles.add(i);    
          }
        }


          try{
            var files = f.listFiles();
            while(files!=null) {
              for (File fl : files) {
                System.out.println(fl.getCanonicalPath());
              }
            }       
          }
          catch(Exception e)
          {
          }
       searchedFiles.remove(i);
      }
    }
  }
I expected it to output "diskLocation\\" then exit but it continuously prints out the files of the Java File(directory) specified by the diskLocation String and I dont understand why?Am I not removing it off the searchedFiles dequeue?What is happening?
Anyway, back to the problem. A "relative path" is a partial path relative to some arbitrary directory in the tree. Your app when it starts will have a "current directory" that is defaulted in some way, it could be the directory where the executable is situated but might not be, you'd need to know that in order to do the processing you're trying to do. All relative paths are resolved to that "current directory" or "current folder".

A file has a different "relative path" depending on what we are relative to. Can you give a few basic example of what you'd expect to see given some directory tree? I mean are you trying to find all occurrences of some file name within a disk? or all occurrences of a relative path within a disk?
 
Last edited:

Thread Starter

Samantha Groves

Joined Nov 25, 2023
152
Anyway, back to the problem. A "relative path" is a partial path relative to some arbitrary directory in the tree. Your app when it starts will have a "current directory" that is defaulted in some way, it could be the directory where the executable is situated but might not be, you'd need to know that in order to do the processing you're trying to do. All relative paths are resolved to that "current directory" or "current folder".

A file has a different "relative path" depending on what we are relative to. Can you give a few basic example of what you'd expect to see given some directory tree? I mean are you trying to find all occurrences of some file name within a disk? or all occurrences of a relative path within a disk?
all of ocurrencies in the disk
 

Thread Starter

Samantha Groves

Joined Nov 25, 2023
152
Anyway, back to the problem. A "relative path" is a partial path relative to some arbitrary directory in the tree. Your app when it starts will have a "current directory" that is defaulted in some way, it could be the directory where the executable is situated but might not be, you'd need to know that in order to do the processing you're trying to do. All relative paths are resolved to that "current directory" or "current folder".

A file has a different "relative path" depending on what we are relative to. Can you give a few basic example of what you'd expect to see given some directory tree? I mean are you trying to find all occurrences of some file name within a disk? or all occurrences of a relative path within a disk?
Well I have the system library filled with the directory as,a song and text file HelloWorld.The directory as has itself a text file HelloWorld.

Given the search path "HelloWorld" I am trying to print out ...\\Music\\HelloWorld.txt and ...\\Music\\as\\HelloWorld.txt but in the output keeps looping on ..\\Music\\HelloWorld endl; ...\\Music\\song.mp3 endl; \\Music\\as. I dont know why tho.
 
Top