• Some users have recently had their accounts hijacked. It seems that the now defunct EVGA forums might have compromised your password there and seems many are using the same PW here. We would suggest you UPDATE YOUR PASSWORD and TURN ON 2FA for your account here to further secure it. None of the compromised accounts had 2FA turned on.
    Once you have enabled 2FA, your account will be updated soon to show a badge, letting other members know that you use 2FA to protect your account. This should be beneficial for everyone that uses FSFT.

Java methods help please

bdario1

n00b
Joined
Mar 24, 2010
Messages
5
This is what i got so far but the problem is i did not realize that we
could not use the ArrayList to search through and now i don't know how
to change it so i don't use it that is the only way i know. Please help

/**

* 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.

* <p>

*

* 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.

* <p>

*

* The files array must not contain null.

* <p>

*

* <p><b>You will write this method as part of programming assignment #9.</b>

*

* @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

*/












Code:





public static File[] getUnique (File[] files)

{

ArrayList<File> output = new ArrayList<File>();



for(int i = 0; i < files.length; i++)

{

boolean copy = false;



for(int j = 0; j < output.size(); j++)

{

if(files.equals(output.get(j)))

{

copy = true;

break;

}

}



if(!copy)

{

output.add(files);

}

}



return (File[]) output.toArray();

}













Requirements:

For getUnique, the input array {new File ("Foo"), new File ("Bar"), new
File ("Foo")} should return an array containing two file objects - File
objects representing files "Foo" and "Bar".

Do not use library classes to manipulate or search through arrays. (The Arrays class is not allowed.)

This method should create and return a new array of File objects. The
array should contain the unique File objects found in the input array.
The input array should not be changed.



Note that you do not need to create new File objects, just a new array of File objects.
 
He only joined [H] to get help on his homework...

Honestly I think you'll learn the material better if you research online / pay attention to what they're asking you to do... most of these first functions are pretty simple once you understand what's going on...

I don't think you ever learn anything if people just tell you what you're doing wrong and how to fix it. I learn so much better on my own if I spent a few hours trying to work through a programming assignment to finally figure it out, feels awesome.
 
Yes i have joined to get hw help too, but i want to learn also, not everyone learns by trial and error if you are not aware of that, and i didn't shoot a question without trying it first, if you don't know how to help you don't have to post comments that are not related to the question. And i would think that if other people add some ideas then it helps me solve and look at the problem with more clarity.
 
Please place code tags around your code.

Example:

Code:
// code here

[/[i][/i]code]
 
I'm not quite sure why you are able to understand how to do it using an ArrayList but can't use the same thinking to do it using plain arrays?

Basically, as I understand it your code should do roughly the following:

Check that the input array files isn't null (throw a NullPointerException, apparently).
Create an output array of type File.
For each of the items in the input array, check that the item is not null (additional check to the first) - if not, compare it to each of the existing items in the output array using .equals - if none of them match then add that input item to the output array.
Return the output array.
 
Yes i have joined to get hw help too, but i want to learn also, not everyone learns by trial and error if you are not aware of that, and i didn't shoot a question without trying it first, if you don't know how to help you don't have to post comments that are not related to the question. And i would think that if other people add some ideas then it helps me solve and look at the problem with more clarity.

Wrong, your suppose to try and try again until you do your own homework, sorry IMO and the sticky's opinion people shoulden't come home to get they're homework done
i suggest reading the ArrayList<Object> API, first, if you don't know how to use them.
 
Back
Top