1
I’m trying to do some tests in JAVA here and I’m in trouble:
The product class, with the initialization and the getters/setters
public class Produtos {
private String[] nomeItem = {"Item 1","Item 2","Item 3","Item 4","Item 5"};
private String[] categoriaItem = {"Utensílio","Utensílio","Utensílio","Utensílio","Utensílio"};
private String[] medidaItem = {"Un","Un","Un","Un","Un"};
private String[] dispobinItem = {"5","5","10" ,"10","20"};
public void iniciaEstoque(String[] nomeItem, String[] categoriaItem, String[] medidaItem, String[] disponibiItem){
setNomeItem(nomeItem);
setCategoriaItem(categoriaItem);
setMedidaItem(medidaItem);
setDisponibiItem(disponibiItem);
}
public String[] getNomeItem() {
return nomeItem;
}
public void setNomeItem(String[] nomeItem) {
this.nomeItem = nomeItem;
}
public String[] getCategoriaItem() {
return categoriaItem;
}
public void setCategoriaItem(String[] categoriaItem) {
this.categoriaItem = categoriaItem;
}
public String[] getMedidaItem() {
return medidaItem;
}
public void setMedidaItem(String[] medidaItem) {
this.medidaItem = medidaItem;
}
public String[] getDisponibiItem() {
return dispobinItem;
}
public void setDisponibiItem(String[] dispobinItem) {
this.dispobinItem = dispobinItem;
}
}
The function for inserting a new element:
public class InsereElementos {
public String[] insereElemento(String[] array, String item){
int indice;
int tamanho = array.length;
String[] arrayDummy = new String[tamanho+1];
for(indice=0;indice<tamanho;indice++){
arrayDummy[indice] = array[indice];
}
arrayDummy[indice] = item;
System.out.println(Arrays.toString(arrayDummy));
return arrayDummy;
}
}
The main:
public class Principal {
public static void main(String[] args) {
String nome = "x";
String[] dummy = new Produtos().getNomeItem();
String[] junta = new InsereElementos().insereElemento(dummy, nome);
new Produtos().setNomeItem(junta);
System.out.println(Arrays.toString(new Produtos().getNomeItem()));
}
}
When executing, a syso in the function you insert shows (returns): [Item 1, Item 2, Item 3, Item 4, Item 5, x]
But in the main while trying to give get in the array that should have been modified syso returns:
[Item 1, Item 2, Item 3, Item 4, Item 5]
Where am I going wrong?
Grateful!
Damn man, thank you very much, it’s been difficult to migrate from C to Java, I’ll try to read more not to ask such silly questions! I also appreciate the respect/sympathy, unlike the little fellow there who doesn’t even know what Syso is. Hugs
– user56548
Dude, no problem, never be intimidated to ask anything here, we are here to precisely build knowledge in community. I hope I’ve been able to help you with your question and answer your question.
– Gabriel Weber