I have an assignment that involves creating three methods that manipulate a linked list. The instructions dictate that I use the following constructor:
public MyList (LinkedList<Integer> list) {
...
}
However, Eclipse seems to not like my code regardless of how I try integrate it. Here's my current attempt:
import java.util.*;
public class ListClass {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(10);
list = MyList(list);
}
public MyList (LinkedList<Integer> list){
LinkedList<Integer> r = list;
return r;
}
}
Now I thought that the MyList constructor above would happily just return the list entered, but my Java skills are really weak. I've been going through the tutorials and gave this a go, but it hasn't worked as I thought it would.
Anyway so Eclipse is giving me two errors - at the "list = MyLIst(list);" line it says the method MyList is undefined for ListClass, and at the "public MyList" line it says "the return type for the method is missing" - but I've told it that r is a linked list, and to return that.
This hurts my brain and I can't manage to figure it out, can anyone give me a hand? I think if I were able to get the above code working, I should be able to get the rest sorted.
Newer code
As rightfully pointed out, my class name isn't the same as my supposed constructor name. So here's the adjusted code:
import java.util.LinkedList;
public class MyList {
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(10);
list.add(-20);
MyList(list);
}
public MyList(LinkedList<Integer> list) {
LinkedList<Integer> newList = list;
System.out.println(newList);
}
}
This has solved the "return type" error (thank you), though I'm still getting the "undefined" error.
What's missing in the declaration of your method MyList is the return type of the method:
public MyList (LinkedList<Integer> list)
should be something like
public LinkedList<Integer> MyList (LinkedList<Integer> list)
Besides that, the usual convention for method names is camel case, but starting with a lower-case letter. I'd call it myList instead of MyList (you should choose a better name for the method that reflects what the purpose of the method is).
If
public MyList (LinkedList<Integer> list) {
...
}
is supposed to be a Constructor, the class also must be named MyList. You can't return anything from a constructor, so just leave the declaration of it as it is.
Just rename your class, save the LinkedList to a private field in the constructor above, and then add the methods you are supposed to implement to the MyList class.
To get rid of the undefined problem, you need to create your list using 'new':
MyList myList = new MyList(list);
With your modified code, there's still a few things to correct:
In Java, you call a constructor in order to create a new Object. You probably want to keep this object when you create it as part of your main() method, using something like the following in order to prevent your 'undefined' error:
MyList ml = new MyList(list);
As part of your Constructor you only store the LinkedList<Integer> that's passed in as as local variable, and not as a class variable. Correct this with the following declaration at the top of your class:
public class MyList {
private LinkedList<Integer> list;
//...
Structure for additional functionality
In order to add the additional functionality as described in your comment below, I'd use the following sort of structure (Obviously you still need to implement the methods, but you can see where I'd put them):
import java.util.LinkedList;
public class MyList {
private LinkedList<Integer> list;
public MyList(LinkedList<Integer> list) {
this.list = list;
}
public LinkedList<Integer> reverse() {
//Create a reversed list
return rList;
}
public LinkedList<Integer> odd() {
//Create a list of the odd elements
return oddList
}
public LinkedList<Integer> even() {
//Create a list of the even elements
return evenList;
}
#Override
public String toString() {
return list.toString();
}
public static void main(String[] args) {
LinkedList<Integer> list = new LinkedList<Integer>();
list.add(0);
list.add(2);
list.add(4);
MyList ml = new MyList(list);
System.out.println("MyList: " + ml);
LinkedList<Integer> tsil = ml.reverse();
System.out.println("Reversed: " + tsil);
LinkedList<Integer> ls = ml.odd();
System.out.println("Odd: " + ls);
LinkedList<Integer> it = ml.even();
System.out.println("Even: " + it);
}
}
The problem here is that a constructor must have the same name of its enclosing class. However, you're trying to name a MyList constructor inside a class named ListClass.
So, either name both your class and the constructor MyList or name them ListClass.
As for the "undefined" issue, you can't directly call a constructor. You have to use it in a "new" statement, as it is used to create new instances of the class:
MyList someList = new MyList(); // variable someList will hold a new MyList instance
or
new MyList(); // instance without a reference variable.
Related
If the list is final can it be copied to another list so if we make changes in new list the original one doesn't get affected ?
public class Solution {
// DO NOT MODIFY THE LIST
public int maximumGap(final List<Integer> a) {
if(a.size()<2)return 0;
List<Integer> c=new List<Integer>(a);
Collections.sort(c);
return c.get(c.size()-1)-c.get(c.size()-2);
}
}
The compiler must be stating
Cannot instantiate the type List
since List is an interface and not an class.
A class must be given like in
List<Integer> c = new ArrayList<>(a);
If you copy a list into another one, then they are independent regardless of the final keyword.
import javax.swing.*;
import java.util.*;
import java.awt.*;
public class ConverAryToList {
public static void main(String[] args) {
// TODO Auto-generated method stub
String[] stuff = { "people ", "watermelon ", "melons ", "fudge " };
List<String> list = new java.util.LinkedList<String>(
Arrays.asList(stuff));
list.add("pumpkin");
list.addFirst("first");
stuff = list.toArray(new String[list.size()]);
for (String x : stuff)
System.out.printf("%s", x);
}
}
This is a simple exercise on adding elements to a list. On list.addFirst I get: The method addFirst(String) is undefined for the type List. Where did I go wrong?
Your reference type is the List<T> interface, which does not declare any addFirst method.
You can either set the reference type explicitly as LinkedList<String>:
LinkedList<String> list = new LinkedList<String>(
Arrays.asList(stuff));
Or you can cast:
((LinkedList<String>)list).addFirst("first");
The first methodology is preferred.
Casting is slightly more "dangerous", because it assumes your reference has not been assigned a new value.
For instance, if you assigned list to new ArrayList<String>(); in between the two statements above, you'd get a ClassCastException at runtime.
method addFirst exists on class LinkedList, not on interface List.
Here :
List<String> list = //whatever
list is considered as a List, not a LinkedList
Change it to :
LinkedList<String> list = //whatever
LinkedList implement two interfaces: List And Deque.
Method addFirst is declared in Deque interface.
Deque list = new LinkedList<Number>();
list.addFirst(3);
say, I made an arraylist in (public class class1(String args[]))
static List<Double> list1 = new ArrayList<Double>();
then I pass this arraylist to a function in (public static void main(String args[]))
biggestvalue(list1);
this is the function for example:
public static double biggestvalue(List<Double> list){
Collections.sort(list);
return list.get(list.size()-1);
}
I pass it into a function so that hopefully it will only sort list but not list1, but then list1 gets sorted as well, and I do not understand why that is.
Therefore, please explain to me why that is, and what solutions to this error are out there?
You only pass a reference to the List when you pass it as an argument. Therefore, both list and list1 point to the same List.
A good rule of thumb is to not modify objects passed into a method, so I would make a copy inside the method:
public static double biggestvalue(List<Double> list){
List<Double> temp = new ArrayList<>(list);
Collections.sort(temp);
return temp.get(temp.size()-1);
}
list1 gets sorted as well because you are passing a reference to that object. You want to duplicate the list before sorting it, so the original object doesn't get modified:
List<Double> dup = new ArrayList<>(list);
Collections.sort(dup);
return dup.get(dup.size() - 1);
This is my code for the class, ListOfLists. The constructor should make an array of type NameList.
public class ListOfLists {
private int capacity;
private NameList[] listOfLists;
private int size = 0;
public ListOfLists(int capacity) {
listOfLists = new NameList[capacity];
}
My NameList class looks something like this..
public class NameList{
public NameList(String initial){
i = initial;
}
public void add(String data){
...
}
If I make a new object in the Main of ListOfLists called k..
ListOfLists k = new ListOfLists(5);
How come I cannot do..
k.add("Whatever") ?
I get the error..
The type of the expression must be an array type but it resolved to ListOfLists
How come I cannot do..
because you don't have add method in ListOfLists class.
If you want to use add method of class NameList then get the value of listOfLists which is of type NameList and then add the Whatever.
k which is of type ListOfLists is something you wrote yourself, and doesn't extend anything. If you didn't write an add method, you can't call it. If you want a list that also has other properties, try extending ArrayList in your ListOfLists class.
I was just playing around and a thought came to my mind and I decided I want to try it:
Make an ArrayList that holds more ArrayLists.
For example, I created an ArrayList called intList that holds ints, then filled it with values. After that I did a stringList one and filled it too. Then I made an ArrayList that holds other ArrayLists called aList and added intList and stringList to it.
Now the problem I faced was if I was retrieving objects from aList, it would not recognize if the generic type was int or string.
Here is the code I tried:
import java.util.*;
public class Practice {
public static void main(String[] args) {
List<ArrayList> list = new ArrayList<ArrayList>();
ArrayList<int> intList = new ArrayList<int>();
intList.add(1);
intList.add(2);
intList.add(3);
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("One");
stringList.add("Two");
stringList.add("Three");
list.add(intList);
list.add(stringList);
for(ArrayList lst : list) {
for(ArrayList lt : lst) {
System.out.println(lt);
}
}
}
}
Java has "generic type erasure", meaning that the type parameters to generics are "erased". Once you create an ArrayList<T> there's no way to find out what T was.
Only class types can be used as generic type parameters, so you can't have an ArrayList<int>. Use an ArrayList<Integer> instead.
In addition, the types used in your loops are wrong. Since list is a list of lists of values, lst is a list of values, which means that your lt variable will be either an integer or a string, not another ArrayList.
The deeper problem here is that you're still using raw types, so the compiler can't find that error for you. You should declare list as something like List<List<? extends Object>>. That way you can add both an ArrayList<Integer> and an ArrayList<String> to it, and extract the values as type Object within your loop.
Since no type information is stored in generic type, you could get element from sub-list and check it's type:
for(ArrayList subList : list) {
if (subList.size() > 0) {
Class elementClass = subList.get(0).getClass();
// do something else with it
}
}
But:
It will not work, if subList is empty
Generally, the concept of storing several lists of different types in another list looks rather strange.
Type erasure means that at runtime, the type is erased. That's why you can cast from one generic to another:
import java.util.ArrayList;
public class Test {
public static void main(String [] args){
ArrayList<String> list = new ArrayList<String>();
ArrayList list2 = (ArrayList)list;
list2.add(new Integer(5));
System.out.println(list2.get(0).getClass());
}
}
Will output:
class java.lang.Integer
import java.util.*;
public class Practice {
public static void main(String[] args) {
List<ArrayList<?>> list = new ArrayList<ArrayList<?>>();
ArrayList<Integer> intList = new ArrayList<Integer>();
intList.add(1);
intList.add(2);
intList.add(3);
ArrayList<String> stringList = new ArrayList<String>();
stringList.add("One");
stringList.add("Two");
stringList.add("Three");
list.add(intList);
list.add(stringList);
for(ArrayList<?> lst : list) {
for(Object lt : lst) {
System.out.println(lt);
}
}
}
}