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.
Related
I have created a custom ArrayList object and recieve an error when trying to cast to this object. I think I have misunderstood something as I expected this to work. If I have a custom ArrayList object that will only treat an ArrayList of Integers:
public class CustomArrayList extends ArrayList<Integer>{
public void customMethod() {
// do things with integer arraylist
}
}
I expect that I could cast a List of Integers like the following:
List<Integer> myList = new ArrayList<>();
((CustomArrayList) myList).customMethod();
But this results in a cast class exception. Can someone please explain what I am doing wrong and how to successfully achieve the cast? Thanks
Your CustomArrayList is an ArrayList<Integer>, but an ArrayList<Integer> is not a CustomArrayList.
If you want to convert an arbitrary ArrayList<Integer> to a CustomArrayList, you can write:
List<Integer> myList = new ArrayList<>();
CustomArrayList customList = new CustomArrayList(myList);
customList.customMethod();
This will require adding a constructor to CustomArrayList that accepts a Collection<Integer> and passes it to ArrayList's public ArrayList(Collection<? extends E> c constructor.
public CustomArrayList(Collection<Integer> c) {
super(c);
}
Note that the CustomArrayList instance created with this constructor is a copy of the original ArrayList, so changes in that instance won't be reflected in the original List.
I am trying do something like this:-
public static ArrayList<myObject>[] a = new ArrayList<myObject>[2];
myObject is a class. I am getting this error:- Generic array creation (arrow is pointing to new.)
You can't have arrays of generic classes. Java simply doesn't support it.
You should consider using a collection instead of an array. For instance,
public static ArrayList<List<MyObject>> a = new ArrayList<List<MyObject>();
Another "workaround" is to create an auxilliary class like this
class MyObjectArrayList extends ArrayList<MyObject> { }
and then create an array of MyObjectArrayList.
Here is a good article on why this is not allowed in the language. The article gives the following example of what could happen if it was allowed:
List<String>[] lsa = new List<String>[10]; // illegal
Object[] oa = lsa; // OK because List<String> is a subtype of Object
List<Integer> li = new ArrayList<Integer>();
li.add(new Integer(3));
oa[0] = li;
String s = lsa[0].get(0);
There is a easier way to create generic arrays than using List.
First, let
public static ArrayList<myObject>[] a = new ArrayList[2];
Then initialize
for(int i = 0; i < a.length; i++) {
a[i] = new ArrayList<myObject>();
}
You can do
public static ArrayList<myObject>[] a = (ArrayList<myObject>[])new ArrayList<?>[2];
or
public static ArrayList<myObject>[] a = (ArrayList<myObject>[])new ArrayList[2];
(The former is probably better.) Both will cause unchecked warnings, which you can pretty much ignore or suppress by using: #SuppressWarnings("unchecked")
if you are trying to declare an arraylist of your generic class you can try:
public static ArrayList<MyObject> a = new ArrayList<MyObject>();
this will give you an arraylist of myobject (size 10), or if u only need an arraylist of size 2 you can do:
public static ArrayList<MyObject> a = new ArrayList<MyObject>(2);
or you may be trying to make an arraylist of arraylists:
public static ArrayList<ArrayList<MyObject>> a = new ArrayList<ArrayList<MyObject>>();
although im not sure if the last this i said is correct...
It seems to me that you use the wrong type of parenthesis. The reason why you can't define an array of generic is type erasure.
Plus, declaration of you variable "a" is fragile, it should look this way:
List<myObject>[] a;
Do not use a concrete class when you can use an interface.
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.
I am trying do something like this:-
public static ArrayList<myObject>[] a = new ArrayList<myObject>[2];
myObject is a class. I am getting this error:- Generic array creation (arrow is pointing to new.)
You can't have arrays of generic classes. Java simply doesn't support it.
You should consider using a collection instead of an array. For instance,
public static ArrayList<List<MyObject>> a = new ArrayList<List<MyObject>();
Another "workaround" is to create an auxilliary class like this
class MyObjectArrayList extends ArrayList<MyObject> { }
and then create an array of MyObjectArrayList.
Here is a good article on why this is not allowed in the language. The article gives the following example of what could happen if it was allowed:
List<String>[] lsa = new List<String>[10]; // illegal
Object[] oa = lsa; // OK because List<String> is a subtype of Object
List<Integer> li = new ArrayList<Integer>();
li.add(new Integer(3));
oa[0] = li;
String s = lsa[0].get(0);
There is a easier way to create generic arrays than using List.
First, let
public static ArrayList<myObject>[] a = new ArrayList[2];
Then initialize
for(int i = 0; i < a.length; i++) {
a[i] = new ArrayList<myObject>();
}
You can do
public static ArrayList<myObject>[] a = (ArrayList<myObject>[])new ArrayList<?>[2];
or
public static ArrayList<myObject>[] a = (ArrayList<myObject>[])new ArrayList[2];
(The former is probably better.) Both will cause unchecked warnings, which you can pretty much ignore or suppress by using: #SuppressWarnings("unchecked")
if you are trying to declare an arraylist of your generic class you can try:
public static ArrayList<MyObject> a = new ArrayList<MyObject>();
this will give you an arraylist of myobject (size 10), or if u only need an arraylist of size 2 you can do:
public static ArrayList<MyObject> a = new ArrayList<MyObject>(2);
or you may be trying to make an arraylist of arraylists:
public static ArrayList<ArrayList<MyObject>> a = new ArrayList<ArrayList<MyObject>>();
although im not sure if the last this i said is correct...
It seems to me that you use the wrong type of parenthesis. The reason why you can't define an array of generic is type erasure.
Plus, declaration of you variable "a" is fragile, it should look this way:
List<myObject>[] a;
Do not use a concrete class when you can use an interface.
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.