Aasically I'm trying to add an item from ArrayList a (allApps) to ArrayList b (myApps) but I'm getting an error.
This is what I'm trying:
public ArrayList myApps = new ArrayList();
#Override
public void onCreate(Bundle savedInstanceState) {
...
for(final ResolveInfo app : allApps) {
addApp(app);
}
}
public void addApp(ResolveInfo app) {
ArrayList.add(app); // ERROR: Cannot make a static reference to the non-static method add(Object) from the type ArrayList
}
What does this error mean and how can I get I copy an item from one array to the other?
add method of ArrayList is not static method so you can not call outside an instance of ArrayList. Declare instance first then call add
it should be myApps.add(app);
You need to call add on an instance of ArrayList, not on the class itself, given your description, what you're looking for is
public void addApp(ResolveInfo app) {
myApps.add(app);
}
Change you method like this:
public void addApp(ResolveInfo app) {
myApps.add(app);
}
Related
I've been asked to do something weird and I need to make a class that is a word set (for a spell checker) and I have to do it using a linked list.
What I've tried for the constructor is this:
public WordSet(LinkedList<String> list) {
LinkedList<String> wordSet = list;
}
But this doesn't let me reference the wordset in the rest of the class. BTW this class doesn't have a main or anything like that
its essentially just a data structure which wraps around a linked list (no I have no idea why they want me to do it).
Can someone tell me what I'm doing wrong here?
As an example of a method in this class, one is:
public void insertWord(String s){
}
where I have to add a word to the wordset, now I know that linked lists have this functionality already
in them but I don't know how to reference a linked list from a constructor because of course the linked list hasn't been instantiated, and can't be because this has no Main() method and I can't just go referencing it from the Class that does have a main method because that's messy.
Create a LinkedinList as a class atribute then try to initialitate it to the constructor so u can after use it when u create an object of the current class
public class WordSet {
private LinkedList<String> list;
public WordSet() {
list = new LinkedList<>();
}
public void insertWord(String s){
list.add(s);
}
What you can do is something like this. First create a class that will have reference variable of your list and then a method for inserting new words. When creating a new object, we want user to "provide" a list on which he/she will work later. Meaning each user will have different list - which is why our constructor has argument of type List.
public class Main {
List<String> words;
public Main(List<String> words) {
this.words = words;
}
public void insertWord(String s){
words.add(s);
}
}
You then create your own list and put that same list inside constructor. Once you have constructed an object, you can insert new words inside your list.
class Test{
public static void main(String[] args) {
List<String> myWords = new LinkedList<>();
myWords.add("table");
myWords.add("window");
myWords.add("car");
Main obj = new Main(myWords);
obj.insertWord("carpet");
//shows all your words
System.out.println(myWords);
}
}
I am trying to add an element to an ArrayList using user input. The problem is when I try add something and ask to list it, it doesn't show it in the list.
I thought it was a problem with the read method, but I am not sure if there is anything wrong with it. The other thing was the fact that the method for adding an element wasn't in a loop, I tried using a loop but it still wasn't working.
There is a movie class with a constructor that has the parameters for title, year, genre, price and a toString method.
Expected result: After adding a movie, it should list the movie added.
Actual result: The add method asks for input but when I use the list method it doesn't list what I added.
Here is the full Kiosk and Catalogue class for more context.
new Catalogue().addMovie();
You are creating a new Catalogue each time you want to add a Movie, and you are never referencing it.
Instead, add all your movies to the same Catalogue:
private void addMovie(Catalogue c) {
c.addMovie();
}
private void listMovie(Catalogue c) {
c.listMovie();
}
that's because of the 'new' keyword. you need to use singleton 'Catalogue' object here.
class Kiosk {
private static Catalogue catalogue;
public Catalogue getCatalogue() {
if(Objects.isNull(catalogue)){
catalogue = new Catalogue();
}
return catalogue; //will return singleton catalogue object
}
private void addMovie() {
getCatalogue().addMovie();
}
private void listMovie() {
getCatalogue().listMovie();
}
}
In Kiosk you must keep an instance of your catalogue.
I don't know where you plan to create the Catalogue instance so added 2 constructors:
public class Kiosk {
private Catalogue cat;
public Kiosk() {
this(new Catalogue());
}
public Kiosk(Catalogue catalogue) {
this.cat=catalogue;
}
private void addMovie() {
cat.addMovie();
}
private void listMovie() {
cat.listMovie();
}
}
Say I've got a Java file with 500 custom objects of type Item. Now, say I want users to be able to add/remove any one of those objects to that list in the program. I want to try to avoid doing something like this:
public class Inventory {
public static ArrayList<Item> inv = new ArrayList<>();
public static void addItem1 {
inv.add(Item.Item1); //Pulling from Item class
} //addItem1()
public static void removeItem1 {
inv.remove(Item.Item1);
} //removeItem1()
public static void addItem 2 {
. . .
}
. . .
}
By doing that, I'd have to make an add and a remove function for every single item. There will be hundreds of items, so I sincerely hope there's a better way for the user to be able to do so from inside of the program. This would further be awful because of how it would require some massive nested switch statements to swap out everything.
Instead I'd hope to implement a single adder method and a single remover method, that could take the user's input (a String with the literal name of the Item they are trying to add), and somehow find/select the Item. Implementation thoughts?
How about using generic class ?
public class Inventory<T> {
public static ArrayList<Item> inv = new ArrayList<>();
public void addItem (T item){
inv.add((Item)item); // where item can be anything from Item.item1 to Item.item500
}
public void removeItem (T item){
inv.remove((Item)item);
}
In that case, to see if your item is in fact an item do something similar to this: System.out.println(item.getClass().getName()); //should return Item
Perhaps use:
public void setItem(Item item){
inv.add(item);
}
Then use the same concept for removal.
I have an MVP setup where the presenter populates two RecyclerViews separately with the following methods:
#Override
public void populateFollowsRV(List<FollowsRVModel> userDataList) {
followsList.addAll(userDataList);
followsAdapter.notifyDataSetChanged();
}
#Override
public void populateFollowedByRV(List<FollowsRVModel> userDataList) {
followedByList.addAll(userDataList);
followedByAdapter.notifyDataSetChanged();
}
I have a separate third method in which I would like to retrieve the lists contents of followsList and followedByList, I have tried to declare a global variable called listOne in the Fragment class and then storing the contents of followsList/userDataList when populateFollowsRV() is called in order to have the contents accessible by other methods in the class but this doesn't seem to work:
//Declare listOne inside the Fragment class
private List<FollowsRVModel> listOne = new ArrayList<>();
#Override
public void populateFollowsRV(List<FollowsRVModel> userDataList) {
followsList.addAll(userDataList);
followsAdapter.notifyDataSetChanged();
//Assign the contents of userDataList to listOne when the presenter calls populateFollowsRV()
listOne = userDataList;
}
listOne still appears to be empty after this.
Third Method
public void populateNonFollowersRV() {
//To test wether listOne has any content
String.valueOf(listOne.size()));
}
I was wondering if it was possible to call the methods of an external class without actually having to declare an object of that class. They way I've got it set up causes the ArrayList stored within the object empties every time the method the object is used in is called.
If I can call the method without an object, then I can fix my problem.
Thanks in advance.
calling class:
public class BookingScreen extends Activity {
GAClass sendApplication = new GAClass();
#Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_booking_screen);
}
public void saveBookingInfo(View view) {
EditText applicantNameText = (EditText) findViewById(R.id.applicantNameTextField);
EditText itemToBurnText = (EditText) findViewById(R.id.itemToBurnTextField);
String appName = applicantNameText.getText().toString();
String appItemToBurn = itemToBurnText.getText().toString();
if (appItemToBurn.isEmpty() || appName.isEmpty()) {
Toast.makeText(BookingScreen.this, "Please fill in all fields.", Toast.LENGTH_SHORT).show();
}
else {
sendApplication.storeApplication(appName, appItemToBurn);
this.finish();
}
}
External method class:
public class GAClass {
ArrayList<Application> peopleAttending;
public void storeApplication(String name, String item){
peopleAttending = new ArrayList<>(10);
peopleAttending.add(new Application(name, item));
}
}
You can do something like below
public class GAClass {
public static ArrayList<Application> peopleAttending=null;
public static void storeApplication(String name, String item){
if(null==peopleAttending){
peopleAttending = new ArrayList();
}
peopleAttending.add(new Application(name, item));
}
}
You can invoke above method like below
GAClass.storeApplication(str_name,str_item);
when you make peopleAttending arraylist static it can be access in static method and
if(null==peopleAttending){
peopleAttending = new ArrayList();
}
Above code ensure first time initialization if peopleAttending 9s null
Use static methods. You can call a static method without creating object of enclosing class.
https://docs.oracle.com/javase/tutorial/java/javaOO/classvars.html
What exactly are you trying to achieve?
The static methods in a class would not need an instance of the class so you can make the methods you need (that do not require the state of the object - i.e. do not need a particular object to work on) static and call them like this:
ClassWithStaticMethods.staticMethod() ;