Show different data in 2 separate JTables - java

The Array data[][] stores the right values inside the loop.
Then I add the array to an ArrayList, but it's like all objects inside the ArrayList are being updated when I change the value of my data array after that.
How can I store the value of each Object[][] separately?
for (int i = 0; i < Materia.length; i++) {
for (int j = 0; j < Materia[i].Aluno.length; j++) {
data[j][0] = Materia[i].Aluno[j].Nome;
System.out.println(data[j][1] = Materia[i].Aluno[j].Nome);//checking outpit, its displaying the correct data I want
data[j][1] = Materia[i].Aluno[j].nota;
data[j][2] = Materia[i].Aluno[j].frequencia;
}
tabs.add(data); //arraylist to storer object data
tabela[i] = new JTable((Object[][]) tabs.get(i), Names);//populate default table model
conteudo2[i] = new JPanel();
conteudo2[i].add(new JLabel(Materia[i].Nome));
conteudo2[i].add(new JScrollPane(tabela[i]));
}

You add your data array (of arrays) to the tab-List.
Since you don't create a new object before adding it to the arrayList, it's the same object, and therefore you also update the contents in the data-Array, when you update the object in the tab-List.
You need to create a new data-Array, before adding it to the list.
You can achieve this by creating a new 2d-Array with the method System.arrayCopy before you add this new object to you tabs-List:
Object[][] myDataCopy = new Object[data.length][];
for(int i = 0; i < data.length; i++) {
myDataCopy[i] = new XXYY[d.length];
System.arraycopy(data[i], 0, myDataCopy[i], 0, data[i].length);
}

Related

how to sort String array Number and bind in array list in android

List<String> list2 =new ArrayList<String>();
int iArr[] = new int[ja.length()];//{"846001","846005","846000","846002","846009"}
Arrays.sort(iArr);
for (int i = 0; i < ja.length(); i++) {
_jobject = ja.getJSONObject(i);
iArr[i] = Integer.parseInt(_jobject.getString("Pincode"));
}
for(int k=0;k<iArr.length;k++) {
list2.add(String.valueOf(iArr[k]));
}
I want to sort and bind it in Array list. I want
{"846000", "846001", "846002" ,"846005", "846009"}
but its not sorting according to given logic please suggest me where am doing wrong.
I think the issue is that you are sorting the array before manipulating it. You should move Arrays.sort(iArr) to after the for loop.
List<String> list2 =new ArrayList<String>();
int iArr[] = new int[ja.length()];//{"846001","846005","846000","846002","846009"}
for (int i = 0; i < ja.length(); i++) {
_jobject = ja.getJSONObject(i);
iArr[i] = Integer.parseInt(_jobject.getString("Pincode"));
}
Arrays.sort(iArr);
for(int k=0;k<iArr.length;k++) {
list2.add(String.valueOf(iArr[k]));
}
The values are accessed as String so convert it to Integers and then sort it. As of now you are sorting it before you populate it properly.
for (int i = 0; i < ja.length(); i++) {
_jobject = ja.getJSONObject(i);
iArr[i] = Integer.parseInt(_jobject.getString("Pincode"));
}
Arrays.sort(iArr);
for(int k=0;k<iArr.length;k++) {
list2.add(String.valueOf(iArr[k]));
}
Let's simulate how your given logic will be executed when it comes to the processor.
Line 1: initialized an empty string type ArrayList
Line 2: initialized an empty int array, with the length of ja.length() long
Line 3: you sort the empty int array
Line 4 to line 7: assign Pincode to your int array sequentially from the json
We have no idea what those json object stored, but apparently they are not sorted. When they get copied to your int array, the int are not sorted as well.
Move line 3 after the line 4 to 7 for loop should do the job. You should sort the array when there is some element inside. Sort an empty has no effect.

Removing one object from multiple ArrayLists

Is there a way to make it so that, by deleting one object from one ArrayList, it deletes it from all the other ArrayLists that it exists in?
So for example in the code below, if I deleted everything from the ArrayList fives, the ArrayList test1 would contain no NumHolders that contained the value 5.
public static void main(String[] args) throws FileNotFoundException{
ArrayList<NumHolder> test1 = new ArrayList<NumHolder>();
ArrayList<Five> fives = new ArrayList<Five>();
String all = null;
for (int i = 0; i < 90; i++){
NumHolder number = new NumHolder(i);
test1.add(number);
if(i%5==0){
fives.add(number);
}
for (int j = 0; j < fives.size();j++){
fives.remove(0);
}
for (int j = 0; j < test1.size();j++){
test1.get(j).toString();
}
}
its easier than that my friend, just don't add the number numbers that divides on 5 without a reminder to your test1 ArrayList.
ArrayList<NumHolder> test1 = new ArrayList<NumHolder>();
ArrayList<Five> fives = new ArrayList<Five>();
String all = null;
for (int i = 0; i < 90; i++){
NumHolder number = new NumHolder(i);
if(i%5==0){
fives.add(number);
continue;
}
test1.add(number);
}
for (int j = 0; j < fives.size();j++){
fives.remove(0);
}
for (int j = 0; j < test1.size();j++){
test1.get(j).toString();
}
If you want to remove all 5s from both lists just use:
test1.removeAll(fives);
fives.clear();
One thing you could do is, instead of removing your elements from fives, keep them in that list and use the list in a single call on all the other lists:
test1.removeAll(fives);
If you don't want that - if there are too many lists and you don't want to call removeAll() on each list, and you really want all lists to share the elements, then you could wrap the elements in a class that contains a single member variable. Then set the member variable in the wrapper to null, instead of deleting it. Then all lists will be referring to the same wrapper element whose contained value was set to null.

How can I create a two-dimensional array containing ArrayLists?

How can I create a two-dimensional array containing ArrayLists? Something like this :
ArrayList<Character>[][] myArray = new ArrayList<Character>[][];
and would it be ok to do the following :
I need to compare the position of some characters with the position of the buildings in my map. Several buildings can belong to the same tile, but one can be drawn in front of the character and the other behind him. This comparison has to be done all the time in the game, with every character.
I am trying to update an array of characters each time a character is moving from one tile to another. Then the render method should look for how many characters, if any, are in a specific tile, and loop over the characters in this tile to draw them in front or behind the buildings.
Something like this :
//init
ArrayList<Character>[][] arrayOfCharacters = new ArrayList<Character>[][];
//each tile in the map
for (int y = 0; y < 9; y++){
for(int x = 9-1; x >= 0; x--){
if ( arrayOfCharacters[y][x].length > 0 ){
for ( int i=0, i< arrayOfCharacters[y][x].length; i++ ){
//compare which building is in front or behind the characters
//then
characterInThisTile = index of each character in arrayOfCharacters[y][x]
spriteBatch.draw(characterInThisTile, x_pos, y_pos, tileWidth, tileHeight);
}
}
}
}
ArrayList<Character>[][] arrayOfCharacters = new ArrayList[9][9];
for(int i=0;i<arrayOfCharacters.length;i++){
for(int i2=0;i2<arrayOfCharacters[i].length;i2++){
arrayOfCharacters[i][i2]=new ArrayList<Character>(20);
}
}
A two dimensional array is an array of arrays - it means that the structure looks something like:
[0,0][0,1][0,2][0,3] -> sub array 1
[1,0][1,1][1,2] -> sub array 2
[2,0][2,1][2,2][2,3][2,4] - sub array 3
Notice how the number of elements in each sub array does not have to be the same. You could create the above array as (I am using integers your type would vary as necessary):
int[][] a = new int[3][]; // The number of sub arrays or the first argument should be defined.
// The number of elements in each sub array need not be known at compile time though
So if had to do the same thing with an ArrayList, an array inside an array would translate to a list within a list. So you could do something like:
ArrayList<ArrayList<Integer>> arrayList = new ArrayList<ArrayList<Integer>>();
Since an ArrayList object can expand dynamically, the structure would be something like:
Row [0] -> [0][1][2]..... // and so on
Row [1] -> [0][1][2]..... // and so on
Row [2] -> [0][1][2]..... // and so on
Entering elements into this would be done very similarly using nested for loops.
You could make a class and then make Objects using that class that stores an ArrayList<Character> as an instance variable.
First make a class that has a instance variable ArrayList<Character>
that also has a getter, setter and constructor.
//Make Objects that will have an ArrayList<Character>
public class ArrayOfChars {
private ArrayList<Character> list;
//Constructor
public arrayOfChars(){
this.list = new ArrayList<Character>();
}
//Getter
public ArrayList<Character> getList(){
return this.list;
}
//Setter
public void setList(ArrayList<Character> list){
this.list = list;
}
}
You can now use this class to make Objects and store that Objects in a 2D array
These Objects can store and ArrayList<Character> that can be used.
public static void main(String[] args) {
ArrayOfChars[][] myLists = new ArrayOfChars[9][9];
//initialize the 2d array so that it is filled with Empty ArrayList<>'s'
for (int i = 0; i < 9; i++) {
for (int j = 0; j < 9; j++) {
ArrayOfChars thislist = new ArrayOfChars();
myLists[i][j] = thislist;
}
}
//You can now use it like a 2d array of objects
Here are some ways you can use this 2D-Array of ArrayList<Character>
//Iterate like this
for (int row = 0; row < 9; row++) {
for (int col = 0; col < 9; col++) {
myLists[row][col].getList().get(index);
//or
myLists[row][col].setList(list);
}
}
//Add to a list
myLists[2][5].getList().add(new Character('H'));
//Set a list of characters
ArrayList<Character> useThisList = new ArrayList<Character>();
useThisList.add('F');
useThisList.add('G');
useThisList.add('L');
myLists[3][7].setList(useThisList);
}
I'd use list of lists, which is more dynamic.
List<ArrayList<Character>> list =
new ArrayList<ArrayList<Character>>();

refreshing JTable doesn't show correctly data

this function should refresh my JTable:
list = null;
list = (ArrayList<String[]>) interface_engine.getData();
info = new String[list.size()][header.length];
Iterator it = list.iterator();
for (int i = 0; i < list.size(); i++) {
String[] current = (String[]) it.next();
for (int j = 0; j < header.length; j++) {
info[i][j] = current[j];
}
}
DefaultTableModel model = (DefaultTableModel) data_table.getModel();
model.addRow(info);
when i call it for add a new row (getData is a remote method for retrieve data from db), the added row it's not display as a string, but a a variable reference (such as String#128dda...). Where's the problem?
the added row it's not display as a string, but a a variable reference (such as String#128dda...).
Read the DefaultTableModel API. You can't add a 2-Dimensional array using the addRow() method. You can only add one row at a time.
The addRow() method should be inside your loop. Build one row of data, then invoke the addRow() method.

Why 'variable can be only null at this location'?

I have this:
String data[][] = null;
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
data[i][j]= "test";
}
}
But it doesn't work. Variable data is null.
Your first line should be
String data[][] = new String[10][10];
because you have to initialize your array first. Consider the array like a "pointer" in C/C++. You have to write something like String data[][] = new String[10][10];
Arrays in java are kind of objects and have to be allocated with new.
Replace
String data[][] = null;
with
String data[][] = new String[10][10];
Of course is data null, you explicitly said so. If you think that
String data[][] = null;
should initialize a 2-dimensional array and each value to null, you are mistaken.
What you need is for example:
String data[][] = new String[10][10]
this initialized a 2-dimensional array with 100 elements, that is to say: an array with 10 elements, each being an array with 10 elements.
An array in Java is an object, just like any other object, and thus has to be initialized with new. data in your example is a reference to an array which itself consists of references to other arrays (= objects).
That is because you've assigned null to it. You need to create an array and assign that to data instead. Try this instead:
String data[][] = new String[10][10];
Array assign value as a Reference type . to to asign any value in it, you have to first create instance of it otherwise it will give error. so you have to write the following :
String data[][] = new String[10][10];
for (int i = 0; i < 10; i++) {
for (int j = 0; j < 10; j++) {
data[i][j]= "test";
}
}
String data[][] is just a reference that can hold a two dimentional string array. since you have not added any object to the reference, it points to null
so do
String data[][] = new String[10][10];
to add an object in to the reference.
you didn't initialize you array. you can define array size as per your need like given example
2d array it's like table row and column you must initialize first row size then column size
here are the example of initialization
data[][] = new String[5][]; // after this you need to define for 2d array like this
data[0] = new String[2];
data[1] = new String[3];
data[2] = new String[1];
as above the column was dynamic you define the column size as much as you want
another one is
data[][] = new String[5][3] then each row has 3 column

Categories

Resources