Question:
JAVA - I have a question about making a list without using arrays.?
?
2008-12-15 20:59:44 UTC
My teacher wants me to change my TaskDriver so that it prompts user to input the tasks, priority, and complexity, but she wants us to use a do-while? and no arrays. If I do a do-while, the object just replaces itself each time, I need it so that a list shows up of all the tasks entered, but I dont get it.

-------------------Complexity Interface--------------------------------------
public interface Complexity
{
public void setComplexity (int complexity);
public int getComplexity();
}
-------------------Public Interface--------------------------------------
public interface Priority
{
public void setPriority (int priority);
public int getPriority();

}

------------------------------Task method----------------------------------
public class Task implements Priority, Complexity
{

private String myTask;
private int myPriority, myComplexity;

public Task (String chore)
{

myTask = chore;
}

public void setPriority (int level)
{
myPriority = level;
}

public int getPriority()
{
return myPriority;
}

public void setComplexity (int level)
{
myComplexity = level;
}

public int getComplexity()
{
return myComplexity;
}

public String getTask()
{
return myTask;
}

public String toString()
{
return ("Task:" + getTask() + "\tPriority: " + getPriority() + "\tComplexity: " + getComplexity());
}
}

--------------------------------Main DriverTester Class-----------------------------
import java.util.*;

public class TaskDriver
{

public static void main(String[] args)
{
Scanner scan = new Scanner (System.in);
String chore1, str;
int pri1 = 0, com1 = 0;

do
{
System.out.println("Enter task: ");
chore1 = scan.nextLine();

System.out.println("Enter the priority: ");
pri1 = scan.nextInt();

System.out.println("Enter the complexity: ");
com1 = scan.nextInt();

Task task1 = new Task (chore1);

task1.setPriority(pri1);
task1.setComplexity(com1);

System.out.println(task1);

System.out.println("Enter another task? (y/n)");
str = scan.nextLine();
str = scan.nextLine();

}
while (str.equalsIgnoreCase("y"));
}
}
Three answers:
anonymous
2008-12-15 21:09:34 UTC
The List is an interface in the standard Java API. http://java.sun.com/j2se/1.5.0/docs/api/java/util/List.html



So something like:

List myList = new ArrayList();

do {

// code to read all the stuff

myList.add(new Task(chore1));

// ask to continue

} while (str.equalsIgnoreCase("y"));
Cliche
2008-12-16 08:22:28 UTC
You can make a linked list. Basically each instance is an object that stores it's data and the next object. First declare the starting object, then have another variable which holds a reference to the last object you put data in. Then every time somebody gives input, make a new object contained in the current object, and update the variable to the new object. Then later since you saved the starting object you can use it to travel through the list.
huh?
2008-12-16 05:11:20 UTC
Have you studied nodes yet?



you can create non-array lists with those.


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...