How do i convert a Set into 2D array in java? [closed] - java

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
So, i have a Set, where each instance has four fields.
I want to convert it to Array[i][j], where each [j] row would represent an instance.
Edit:
Ok. Sorry for my bad question construction. Im trying to make a programm, which would represent a TreeSet data in table with javax.swing.table.AbstractTableModel.
Actual problem was in AbstractTableModel's getValueAt(int r, int c) method, which needs an index for every table element. Since sets don't have an index, i decided to convert data into 2D array just for table because it makes things simple. But now i'm stuck with setValueAt(Object value,int r,int c), where i should convert edited data back to Set.
Now i'm thinking of converting this Set to List instead of array since it would be easy to transfer back.

Let be a:
public class MyClassWhithFourFields {
String field1;
int field2;
Object field3;
double field4;
}
Now you can declare a set with this template:
Set<MyClassWhithFourFields> mySet = new HashSet<MyClassWhithFourFields>();
In your Set are your objects, which has 4 fields.
If you want to declare a 2 dimension array, than what type should be?
- the most common parent can be, and in this case it is the Object.
So declare a function an implement it:
Object[][] transformSetTo2dArray(Set < MyClassWhithFourFields > mySet) {
if (mySet == null) {
return null;
}
Object[][] result = new Object[4][mySet.size()];
int j = 0;
// iterate the set:
for (MyClassWhithFourFields myObject: mySet) {
myObject[0][j] = myObject.field1;
myObject[1][j] = myObject.field2;
myObject[2][j] = myObject.field3;
myObject[3][j] = myObject.field4;
j++;
}
return result;
}

Related

What is the difference between object and object[] in java [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
So i wrote a method that accepts any java object and i figured out that
public void mymethod(Object javaobject) {
}
works, but with
public void mymethod(Object[] javaobject) {
}
Eclipse trows an error
The method mymethod(Object[]) in the type myClass is not applicable for the arguments (Object)
So my question is, where is the difference between these two types ?
Simply, the difference is that Object is a single object whereas Object[] is an array (multiple or collection) of indexed objects.
For example you could have an object that contains a string like
Object obj = "Hello";
or you could have an array of strings like
Object[] objArray = new Object[2];
objArray[0] = "Hello,";
objArray[1] = " world!";
So, obj is one Object. Whereas, objArray is an array that contains multiple objects indexed starting with 0. Hopefully this helps!
An array is-a an Object, but one Object is not (necessarily) an array of Object(s). You could make your method take varargs,
public void mymethod(Object... javaobject) {
// ...
}
Then it would work like an array (that is javaobject is an array) in both cases (called with one, or more, Object(s)).

How to know if the value of a string is a java data type or not [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 3 years ago.
Improve this question
I have a string tmp like that: String tmp = "value";
I want to know if the value of tmp is a data type in java or not, in this case not because "value" is different then "int" or "double"...
but if tmp hold "int" so it is a data type.
I want to know how to check this, if there is an enumeration of data type of java mention it please.
Java itself doesn't have a built-in list of primitive data types (at least not that I'm aware of), but luckily it's a small and closed list, so you could just create it yourself. For any non-primitive class, you could just attempt to call Class.forName on it:
private static final Set<String> PRIMITIVES =
new HashSet<>(
Arrays.asList("byte", "char", "short", "int", "long", "float", "double", "boolean"));
public static boolean isType(String s) {
if (PRIMITIVES.contains(s)) {
return true;
}
try {
Class.forName(s);
} catch (ClassNotFoundException ignore) {
retrun false;
}
return true;
}

Difference boolean[] b = {false} vs. boolean b = false? [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
What is the difference between:
boolean[] cameraPermissionGranted = {false};
and
boolean cameraPermissionGranted = false;
??
There is nor problem, both work.
I just want to know what is the difference on the memory for instance.
What is the difference regarding prerformance issues..
boolean[] cameraPermissionGranted = {false}; create a boolean array, it's first element is false.
boolean cameraPermissionGranted = false; create a boolean variable, it's value is false.
Difference between these primarily is that
boolean[] cameraPermissionGranted = {false};
is an array that persists boolean type of data initialized with single element false at present unless resized(re-initialized) while
boolean cameraPermissionGranted = false;
is just an attribute that is initialized as false and can be updated thereafter.
One of the very intuitive example that comes to my mind for that is usage in lambda's :
boolean[] cameraPermissionGranted = {false};
boolean cameraPermission = false;
List<Integer> list = new ArrayList<>();
list.forEach(a -> {
cameraPermissionGranted[0] = true; // effectively final
// cannot use cameraPermission
});
The first one creates a single element boolean array, the other creates a boolean type.
The former is often used in places where final is required and element modification is desirable, such as closures, although modern Java versions introduce other more readable and less subversive techniques. It is also somewhat analogous to the reference type of C++: you can pass the array reference to a function and that function can modify the array element and such modifications will be seen by the caller.
One reason of the use of the first one would be to mimic a C++ pass-by-reference. Consider the following example in C++:
void toggleValue(bool &b){
b = !b;
}
It seems hard to do it in Java since you cannot pass a parameter by reference. But with a singleton array, you can mimic the same behavior :
private void toggleValue(boolean[] b){
b[0] = !b[0];
}

How do you know you need to define a new method? [closed]

Closed. This question is opinion-based. It is not currently accepting answers.
Want to improve this question? Update the question so it can be answered with facts and citations by editing this post.
Closed 5 years ago.
Improve this question
We want each method to do a distinct task, right? How do you draw that line?
For example, say I have a class with an int array that I need to have set to certain numbers when the class is constructed. Should I loop through the array right there or make a separate method for what is ultimately a simple task?
Make a new method whenever you have a sensible name for one. When you have a good name for a new method, it suggests that what you're doing there is a separate task and potentially reusable.
Note that this is just a rule of thumb and may not apply to all cases. Another rule is to make a new method if your current method is too long (I've heard 48 lines cited as an upper bound).
Here is a sample class that I hope points out what you are looking for:
class Sample {
private int[] myInts = null;
// we need a constructor if we are going to pass in stuff
// if we don't provide a constructor, java will create one for us
public Sample(int[] inputs) {
// I can just set this array, I don't need a separate method to
// loop through and create a new array and copy the old one.
myInts = inputs;
}
// here we are going to do something discreet, so I need a new method.
public int add() {
int returnValue = 0;
for (int i = 0; i < myInts.length; i++) {
returnValue += myInts[i];
}
return returnValue;
} // end my "add()" method
} // end my Sample class

Using reference to "this" object from member variable [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 6 years ago.
Improve this question
I am asked to say what does this code do but I really cannot figure it out.
I've tried to execute it in netbeans and got the answer 6 but really cannot understand why.
public class Quattro {
int x = 5;
Quattro s = this;
Quattro f(){
s.s.s.x++;
return s;
}
void g(){System.out.println(x);}
public static void main (String[] args){
Quattro a4 = new Quattro();
a4.f().g();
}
}
Question 1: What does Quattro s = this; do? Am I declarind a pointer to my self? If so, it means that I can write
Quattro f(){
s.s.s.x++;
return s;
}
or even
Quattro f(){
s.s.s.s.s.s.s.s.x++;
return s;
}
and I'll always get the same result because I'm in a loop?
Question 2: I do not understand what a4.f().g(); does... seems so weird to me.
If you assign this reference to a member variable, you have a recursion. Yes, it doesn't matter how many s's you'll add, because they are always the same object, which is this object. It's the same as if you wrote:
this.this.this.this.this.this.x++;
Function f() returns reference to this object after doing some other operations on it. It's a common design pattern in Java, called builder. Adding ability to do a4.f().g(); to a class is called method chaining. In other words, f() is this object at the end of the call, just like s is, so you can do:
a1.f().f().f().f().f().f();
And it means you just called f() function from a1 object 6 times.

Categories

Resources