Registered User Registered User - 2 years ago 61
Java Question

What should be imported to create a list?

I attempted creating a list:

List list = new ArrayList();

I compiled it and it said it needed something to import.
I tried
import java.util.Collection;

But that doesn't do anything.

I tried
but all the error messages gave me the impression that it's kinda risky.

Here's my code so you can understand my dilemma, which is a mod of this code:

// purpose: remodify original program such that it will not print duplicate factors. added lines are marked by //t

import java.util.Collection; //???????????????? //t
public class Factors
public static void main(String[] args) {

// command-line argument
long n = Long.parseLong(args[0]);
List list = new ArrayList(); //potato, attempt to create a list so that I can later modify the list such that it will delete dupes right before printing
System.out.print("The prime factorization of " + n + " is: ");

// for each potential factor i
for (long i = 2; i*i <= n; i++) {

// if i is a factor of N, repeatedly divide it out
while (n % i == 0) {
System.out.print(i + " ");
n = n / i;

Set set = new HashSet(list); //t, I read somewhere that this will clear the dupes
// if biggest factor occurs only once, n > 1
if (n > 1){
System.out.println(set); //t, i assume it will do as expected, but right now the import error is stopping me

else System.out.println();

being new to java, I did not know what the hell I was doing. Going back on this question, I believe all I had to do was
import java.util.ArrayList;
and it would work as I needed. Sorry for the trouble.

Answer Source
import java.util.ArrayList;
import java.util.List;

List<data type> var = new ArrayList<data type>();

Example List<String> var = new ArrayList<String>();

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download