Please I need help right away! /** * * This method creates and returns a new arr
ID: 3798051 • Letter: P
Question
Please I need help right away!
/**
*
* This method creates and returns a new array that contains a list of the
* unique File objects that occur in the files array. For this method,
* File objects are the same if they are equal using the .equals method.
* (The returned array is the set of file objects.)
*
*
*
* The size of the returned list is determined by the number of unique
* items in the original list. The order of the items in the returned
* list is unspecified.
*
* do not use library classes to manipulate or search through arrays, or to build a set of objects.
*
* The files array must not contain null.
*
*
*
*
(Hint: This problem also has two major steps. Solve them one at a time,
* and don't try to mix the code between the two steps. (Keep the ideas
* and code steps separate as much as you can.) Additionally, the
* code for each step is very similar.
*
*
(Warning: You cannot use Java collections, or any other built-in
* class that helps build sets. Figure out your own way to see if the array
* has duplicates in it.)
*
* @param files An array of File objects, possibly containing duplicates
* @return An array of unique File objects
* @throws NullPointerException If files is null or files contains null
public static File[] getUniqueSet (File[] files)
{
}
Explanation / Answer
//Please check , Just modify class and package name as per your class name .
package prjctreturnuniquearrayfile;
import java.io.File;
public class PrjctReturnUniqueArrayFile {
public static void main(String[] args) {
// create object
PrjctReturnUniqueArrayFile obj = new PrjctReturnUniqueArrayFile();
// array of original file with duplicate file
File[] arrFiles = obj.getListOfFileArray();
// get unique file from original file
File[] Uniquearrayfile = obj.getUniqueArrayFile(arrFiles);
//Display all unique file for checking or testing
obj.DisplayUniqueFileFromArray(Uniquearrayfile);
}
public File[] getListOfFileArray() {
File file1 = new File("file1");
File file2 = new File("file2");
File file3 = new File("file3");
File file4 = new File("file4");
File file5 = new File("file5");
//file1 and file2 repeated
File[] files = new File[]{file1, file2, file3, file4, file5, file1, file2};
return files;
}
//return an array of unique File objects
public File[] getUniqueArrayFile(File[] files) {
//throws NullPointerException If files array is null
if (files == null) {
throw new NullPointerException("File is null");
}
// store Unique file in array to return
File[] arrNewFile = new File[files.length];
for (int i = 0; i < files.length - 1; i++) {
for (int x = 0; x < files.length - 1; x++) {
if (files[i].equals(files[x]) && (i != x)) {
//Duplicate file
break;
}
if (i == x) {
arrNewFile[i] = files[i];
}
}
}
return arrNewFile;
}
public void DisplayUniqueFileFromArray(File[] files) {
for (int i = 0; i < files.length; i++) {
if (files[i] != null) {
System.out.println(files[i].getName());
}
}
}
}