How do I access this ArrayList from another class? - java

I'm a newbie in java and I have to do a "array project" for college. I'm having trouble getting the array in my Main class from Contacts class where it is stored. This is what I've done for now:
public class Main {
public static void main(String[] args) {
// View Contact Testing
Contacts contactObj = new Contacts();
System.out.println(contactObj);
}
}
public class Contacts {
public static void main(String[] args) {
ArrayList<String> arr = new ArrayList<String>();
arr.add("name1");
arr.add("name2");
return arr.clone();
}
}
When I run this, I get this error: Error:(11, 25) java: incompatible types: unexpected return value.
Any help would be much appreciated,
Thanks

As suggested by others I also advice you study a little bit more in some basic Java tutorials (there is a several spread over www). But also I understanding your struggling and will give you a few example that how you could do that. Follow:
Main.java
public class Main {
public static void main(String[] args) {
Contacts contactObj = new Contacts();
contactObj.getNames().forEach(System.out::println);
contactObj.cleanNames();
contactObj.getNames().forEach(System.out::println);
contactObj.addName("John");
contactObj.addName("Maria");
contactObj.getNames().forEach(System.out::println);
}
}
Contacts.java
import java.util.ArrayList;
import java.util.List;
import java.util.Objects;
public class Contacts {
private List<String> names;
//either to help your understanding and for sake of simplicity
public Contacts() {
this.names = new ArrayList<String>();
this.names.add("name1");
this.names.add("name2");
}
//clean the content of names from names List
public void cleanNames(){
if (Objects.nonNull(names)) {
this.names.clear();
}
}
//add a name to list
public void addName(String name) {
if (Objects.nonNull(names)) {
this.names.add(name);
} else {
this.names = new ArrayList<>();
this.names.add(name);
}
}
//set a entire list to names attribute
public void setNames(List<String> names) {
this.names = names;
}
//get the name attribute
public List<String> getNames() {
if (Objects.nonNull(names)) {
return this.names;
} else {
this.names = new ArrayList<>();
return names;
}
}
}

Related

Creating a method that creates multiple lists inside a class

i wanted to know if you can create a method inside a class that can create more than 1 list
public class UnidadeSaude {
private String NomeUnidade;
public UnidadeSaude() {
}
public UnidadeSaude(String NomeUnidade) {
this.NomeUnidade = NomeUnidade;
}
public String getNomeUnidade() {
return NomeUnidade;
}
public void setNomeUnidade(String nomeUnidade) {
NomeUnidade = nomeUnidade;
}
void gravar(String NomeUnidade){
List<String> UnidadeSaude = new ArrayList<String>();
UnidadeSaude.add(NomeUnidade);
}
void ler() {
System.out.print(UnidadeSaude);
}
}
First: in Java, variable names are written in camelCase and Classes in PascalCase.
This is extremely important to difference between objects and Class references.
Second: I think you're trying to write names into a List in the gravar() method.
You can do it by having a static List in the class. You can add them to the list and then print them in the ler() method.
public class UnidadeSaude {
private String nomeUnidade;
private static List<String> nomeUnidades = new ArrayList();
public UnidadeSaude() {
}
public UnidadeSaude(String nomeUnidade) {
this.nomeUnidade = nomeUnidade;
}
public String getNomeUnidade() {
return nomeUnidade;
}
public void setNomeUnidade(String nomeUnidade) {
this.nomeUnidade = nomeUnidade;
}
void gravar(String NomeUnidade) {
nomeUnidades.add(NomeUnidade);
}
void ler() {
for (String nome : nomeUnidades) {
System.out.println(nome);
}
}
}
HOWEVER, I don't recommend this! It doesn't semantically make sense to store multiple objects into a class that represents a single object. You should ideally store them in a List outside that class

How can I add value to ArrayList in class x while the making object of class x in the main class

In the main class I want to make an object of ChainOfItems class in the meanwhile fill the Arraylist as well. I can fill it with null value but with numbers I cannot do it.
import java.util.*;
public class ChainOfItems {
private ArrayList<Integer> itemes= new ArrayList<>();
public Chain() {
}
public Chain(ArrayList<Integer> itemes) {
super();
this.itemes = itemes;
}
public ArrayList<Integer> getRequiredVNFTypes() {
return itemes;
}
public void setitemes(ArrayList<Integer> itemes) {
this.itemes = itemes;
}
}
public class main {
public static void main(String[] args) {
ChainOfItems item1=new ChainOfItems(null);///what i want to say the list contains two variable 1 and 10
}
}
The constructor and the setitemes (spelling) method accepts an ArrayList.
item1.setitemes(new ArrayList(Arrays.asList(1, 10)));
Or:
ArrayList<Integer> list = new ArrayList<Integer>();
list.add(1);
list.add(10);
item.setitemes(list);
ArrayList<Integer> items= new ArrayList<>();
items.add(1);
items.add(10);
ChainOfItems item1=new ChainOfItems(items);
Above should work for you.
It is much better to use interfaces instead of given implementation (read about OOP concepts). Consider to change class in this way:
public class ChainOfItems {
private List<Integer> items;
public Chain() {
this(new ArrayList<>());
}
public Chain(ArrayList<Integer> items) {
this.items = items;
}
public List<Integer> getRequiredVNFTypes() {
return items;
}
public void setItems(ArrayList<Integer> items) {
this.items = items;
}
}
Now you can provide many implementations of List interface:
ChainOfItems item1=new ChainOfItems(Arrays.asList(1,10));
or
List<Integer> items= new LinkedList<>();
items.add(1);
items.add(10);

Passing data to SOAP web service

I have created a web service and deplyed it on GlassFish on localhost.
#WebService
public class ProductCatalog {
ProductServiceImpl productService = new ProductServiceImpl();
public boolean addProductCategories(String category) {
return productService.addCategory(category);
}
public List<String> getProductCategories() {
return productService.getCategories();
}
}
My service class
public class ProductServiceImpl {
List<String> categories = new ArrayList<>();
public boolean addCategory(String categoryName) {
return categories.add(categoryName);
}
public List<String> getCategories() {
return categories;
}
}
Through the GF admin panel I checked both methods. They work. I can add a category or get all existing categories.
After that I imported the wsdl and got following classes.
Then I created a new project and added imported classes to it.
public class DataController {
public static void main(String[] args) {
String category = "Horror";
ProductCatalog productCatalog = new ProductCatalog() {
public boolean addProductCategories(String arg0) {
return false;
}
public List<String> getProductCategories() {
return null;
}
};
productCatalog.addProductCategories(category);
ProductCatalogService productCatalogService = new ProductCatalogService();
}
}
Now I wan't to add a new category, but have no idea how to do it.
This
productCatalog.addProductCategories(category);
doesn't work. The app runs, but when checking existing categories through GF admin panel, I see that none were added.
What is the reason?
I was using the wrong way. This is how it should be.
public class DataController {
public static void main(String[] args) {
String category = "Anime";
ProductCatalogService productCatalogService = new ProductCatalogService();
ProductCatalog productCatalog = productCatalogService.getProductCatalogPort();
productCatalog.addProductCategories(category);
List<String> categories = productCatalog.getProductCategories();
for(String categoryName : categories){
System.out.println(categoryName);
}
}
}

How to add data to generic array list in java

I am very new in java reflection and generic types. I am now developing database handler library for android. I am stuck at returning array list from reflection.
for example:
This class is the library user's class
public class Creation extends SimpleDBHandler<Creation> {
private String tools;
private String nature;
public Creation(String tools,String nature) {
this.tools = tools;
this.nature = nature;
}
public String getTools() {
return tools;
}
public void setTools(String tools) {
this.tools = tools;
}
public String getNature() {
return nature;
}
public void setNature(String nature) {
this.nature = nature;
}
}
This class is the library user class
usage:
public class Main {
public static void main(String[] args) {
ArrayList<Creation> arrList = new ArrayList<>();
arrList = new SimpleDBHandler<Creation>().getAllData(Creation.class);
}
}
This is library class
public class SimpleDBHandler<T> {
public <T extends SimpleDBHandler<?>> ArrayList<T> getAllData(Class<?> mClass) {
ArrayList<T> arrList = new ArrayList<>();
// I am suck here. How to add data to arrList?
return arrList;
}
}
Please help me. Thanks before.

Output gives nothing

class A contain:
public static ArrayList<String> sourceList = new ArrayList<String>();
// here ArrayList contain some string type data;
public static ArrayList<String> getSource()
{
return sourceList;
}
main class contain:
List<String> s1 = A.getSource();
for(String dk:s1) {
System.out.println(dk);
}
Here I am storing ArrayList data of class A into a list in class B and checking
whether its works or not.but when I run the main class it gives nothing.
thanks.
I've simulated what you are doing, and it's working as expected. Are you sure the list is being populated correctly?
import java.util.ArrayList;
import java.util.List;
public class PrintList {
public static void main(String[] args) {
List<String> s1 = AClass.getSource();
for (String dk : s1) {
System.out.println("We have : " + dk);
}
}
}
class AClass {
public static final ArrayList<String> sourceList = new ArrayList<String>();
static {
sourceList.add("A string");
sourceList.add("Another string");
}
public static ArrayList<String> getSource() {
return sourceList;
}
}
If that is all the code you are using, then you will never get anything since you are never populating the array list in the first place.
If you really want to see if it is working, try something like so:
public static ArrayList<String> sourceList = new ArrayList<String>();
// here ArrayList contain some string type data;
public static ArrayList<String> getSource()
{
sourceList.add("Hello I am working");
return sourceList;
}
If all goes well, you should see the string Hello I am working.

Categories

Resources