Java Array with the same name as a class name - java

I really can't find any information about it that's why I'm asking here.
I'm trying to figure out what is it type of Array if it has same name as a class... like:
public class ArrayOne {
int SomeInt;
ArrayOne [] arr;
public ArrayOne(SomeInt){
arr=new ArrayOne[1];
}
}
Maybe someone know where can I read about it. Thanks a lot

If you declare an array like this:
int[] myIntArray = new int[3];
you have an array of int (remember that int in java is not an object)
So if you write:
ArrayOne[] arr;
You will have an array of object from class ArrayOne.
If you are still in doubt, you can check an array type like this:
Class ofArray = o.getClass().getComponentType();
take a look here : http://docs.oracle.com/javase/8/docs/api/java/lang/Class.html#getComponentType--
public Class getComponentType()
Returns the Class representing the component type of an array. If this class does not represent an array class this method returns null.

That would be an array of that classes objects.

Your arr variable will be of Array type, which contains objects of ArrayOne type.
If the question is around naming, then your array is named arr but not after the class which it is typed with.

Related

How to enter one dimensional array filled with unknown values of variable

I'm a Java beginner and I don't understand how to make it. When I write in my code something like in the example, my IDE underlines it and says it's wrong when I only started writing my code. Can anybody help me guys?
Example:
public class ArrayUtils {
public static int[] lookFor(int[] array) {
int[] array = {};
}
}
The variable named array is already passed in as a parameter. Which means that you cannot create a new int[] named array inside the java method. Try naming it something else.
Syntax with {} means initialization of your array like int[] array = {1,2,3}.
But you can't initialize the variable with the same name as parameter's name.
You can assign a new array to the variable:
public static int[] lookFor(int[] array) {
array = new int[6]; // assign to variable new array with length 6
array = new int[]{1,3,5}; // assign to variable new array with initialized values
}
Note: in first case all 6 values will be zero
Update: as it was mentioned by #ernest_k reassigning method parameters is a bad practice. To avoid it method parameter usually marked as final int[] lookFor(final int[] array)

adding size to private array - java

I'm very new to Java programming and wanted to try my hand at a little bit outside of my classes. I've created a class that will manipulate arrays, so I set up a private array with no size allocated to it. In a public constructor, how do I set the size of this array?
public ClassName()
{
arr = new int[10];
}
Remember that the Constructor is the method called when an object is instantiated. The Constructor must be a method with no return type and the same name as the class. You could even take in parameters if you'd like to(say a size variable), then create a new array based on the size.
For instance, you could do this:
public ClassName(int size)
{
arr = new int[size];
}
Now when in your tester class, you could create a new object using that constructor.
ClassName c = new ClassName(5);
Which creates a new object with an array of size 5 as a class variable. Hope this helped!
Edit: I should add; if you do not specify a constructor, Java will do it for you, but it will do nothing.
Just like in must of the languages.
example:
anArray = new int[10]; //10 - array size, int is the array type
read about JAVA Arrays

What are generic arrays in Java?

I have a code, whose MCVE is like this
import java.util.LinkedList;
public class Test
{
LinkedList<Node>[] arr;
Test(){
arr = new LinkedList<Node>[4];
}
public static void main(){
}
}
class Node{
}
where I get this error
error: generic array creation
arr = new LinkedList<Node>[4];
^
I looked for the error and found these two posts
How to create a generic array in Java?
and Error: Generic Array Creation
But I don't understand what a generic array is. I guess(from what I get from the two posts) it means array that does not know what type it's going to store, but in my case the array is specified to be of LinkedList<Node> type.
Can someone explain the concept?
Please check the following answer:
https://stackoverflow.com/a/18581313/303810
Since it is a bit large, I'll sum up the relevant part here.
LinkedList<Node>[] is a generic array as it has generic type of the component. The problem with this consruct is that you could do the following:
LinkedList<Node>[] listsOfNodes = new LinkedList<Node>[1];
Object[] items = listsOfNodes;
items[0] = new LinkedList<String>();
So at this point listsOfNodes[0] which should have had the type LinkedList<Node> would be an instance of LinkedList<String>.

How do I use an array that is of another class type?

I have declared a class named Member. I then assigned an array with Member type. When I try to put things into the array, it gives me this error:
Exception in thread "main" java.lang.NullPointerException
at HW2_2.main(HW2_2.java:15)
This is my code:
import c.Member;
import java.util.Scanner;
public class HW2_2
{
public static void main(String []args)
{
Member[] p = new Member[100];
Scanner in = new Scanner(System.in);
p[0].setID("apple12");
p[0].setPassword("1234");
p[0].setFirstname("fname");
p[0].setLastname("lname");
p[0].setEmail("*#gmail.com");
}
}
How do I fix this to the point where I can store data into my array?
You have created an object p which points to an array of Member objects. This is perfect. However, each object in your array is by default null. You cannot simply perform operations on them.
You probably want to do something like...
//...
p[0] = new Member(...);
p[0].setId("ID");
//... And so on
What's important to learn from here is that an array declaration syntax does not initialize the values of the array itself. That would be impossible, right? How would you pass arguments to the constructors of each one seperately? You have to do it manually.
When you:
Member[] p = new Member[100];
it initializes all array members to null (since this is an array of object types).
You need to initialize members if you want them to be usable, therefore here you have to:
p[0] = new Member();
before you can use that member.
If you want to initialize all members at once, you need to loop over all elements in the array and create one for each member; with Java 8 this can be done as such:
IntStream.range(0, p.length).forEach(index -> p[index] = new Member());
Curiously enough, there is no such method as <T> void fill(T[] array, Supplier<T> supplier) in the Arrays class.

Calling a Method that has Generic Parameters

I am trying to understand some source code. How do I call this method in main?
public void introSort(T[] array, int max_depth)
{
System.out.println(array.length);
}
I simply want to return the length of the given array in my main method. If this is any help, the method is in a class with this declaration:
public class IntroSort<T extends Comparable<T>> extends AbstractSort<T> {
[I understand that this is of generic type, but my understanding of that concept in Java is too minimal for me to answer my questions.]
The type of the array element is the type variable T of the class IntroSort. This is an example for an instance of IntroSort with the type parameter set as Integer:
Integer[] array = {1, 2, 3, 4};
IntroSort<Integer> obj = new IntroSort<>();
obj.introSort(array, 1);
The compiler would match T with Integer based on the declaration of IntroSort, which also means that the array must be of type Integer.
The T is just referring back to the T in your class specification.
If you have an instance of IntroSort, just call the method on it with the same type of array as you instantiated the class with.

Categories