E
E
Erik Mironov2019-11-18 08:57:52
Java
Erik Mironov, 2019-11-18 08:57:52

How to use my ArrayList in another Java class?

I am new to Java, trying to create multiple ArrayList, how do I call array values ​​in another class?

import java.util.*;
import java.util.ArrayList;

@SuppressWarnings("unused")


public class Objects {
    public class Pair {
        private int key;
        private String value;

        public Pair(int key, String value) {
            this.key = key;
            this.value = value;

        }

       public class itemsNames {
            @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
            public  ArrayList<String> laptopNames() {
                List<String> dell = new ArrayList<>();
                List<String> hp = new ArrayList<>();
                List<String> apple = new ArrayList<>();
                dell.add(0, "DELL Inspiron");
                dell.add(1, "DELL Latitude");
                hp.add(0, "HP Pavilion");
                hp.add(1, "HP_Omen");
                apple.add(0, "McBook Air 11");
                apple.add(1, "McBook Air 13");
                return null;
            }

            @SuppressWarnings("MismatchedQueryAndUpdateOfCollection")
            public void smartPhonesNames(String[] args) {

                List<String> nokia = new ArrayList<>();
                List<String> xiaomi = new ArrayList<>();
                List<String> apple = new ArrayList<>();
                nokia.add(0, "Nokia 3310");
                nokia.add(0, "Nokia 5.1");
                xiaomi.add(0, "Xiaomi redmi note 3 pro");
                xiaomi.add(1, "Xiaomi Mi 9T Pro");
                apple.add(0, "Iphone 8S");
                apple.add(0, "Iphone 11 Pro");


            }
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey Cheremisin, 2019-11-18
@Erik_Mironov

You should read about class fields and their getters/setters, class constructors, initialization of class fields, their visibility. (For example, here - developer.alexanderklimov.ru/android/java/class.php or https://metanit.com/java/tutorial/3.1.php )
For example, you can remake your class like this (this is very hastily, so that clearer).

package com.antek.memgen.gui;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

public class Objects {

  public static class ItemsNames {

    private Map<String, List<String>> laptops = new HashMap<>();
    private Map<String, List<String>> phones = new HashMap<>();

    public ItemsNames() {
      List<String> dell = new ArrayList<>();
      List<String> hp = new ArrayList<>();
      List<String> apple_comps = new ArrayList<>();

      List<String> nokia = new ArrayList<>();
      List<String> xiaomi = new ArrayList<>();
      List<String> apple_phones = new ArrayList<>();

      // initialize computers
      dell.add(0, "DELL Inspiron");
      dell.add(1, "DELL Latitude");
      hp.add(0, "HP Pavilion");
      hp.add(1, "HP_Omen");
      apple_comps.add(0, "McBook Air 11");
      apple_comps.add(1, "McBook Air 13");

      // initialize phones

      nokia.add(0, "Nokia 3310");
      nokia.add(0, "Nokia 5.1");
      xiaomi.add(0, "Xiaomi redmi note 3 pro");
      xiaomi.add(1, "Xiaomi Mi 9T Pro");
      apple_phones.add(0, "Iphone 8S");
      apple_phones.add(0, "Iphone 11 Pro");

      laptops.put("dell", dell);
      laptops.put("hp", hp);
      laptops.put("apple", apple_comps);

      phones.put("nokia", nokia);
      phones.put("xiaomi", xiaomi);
      phones.put("apple", apple_phones);
    }

    public List<String> laptopNames(String brand) {
      return laptops.get(brand);
    }

    public List<String> smartPhonesNames(String brand) {
      return phones.get(brand);
    }

  }

  public static void main(String[] args) {

    // Simple tests

    ItemsNames items = new ItemsNames();

    items.laptopNames("apple").forEach(System.out::println);

    items.smartPhonesNames("apple").forEach(System.out::println);

  }

}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question