A L
A L

Reputation: 31

Initialized HashSet Array in Java

I'm having a problem initializing array of HashSet

  int N = 100;
  HashSet<Integer> []array = new HashSet[N]; 
  for (HashSet<Integer> set:array){
    set = new HashSet<Integer>();
  }

But the array contains only null. (Also error when HashSet []array = .... )

But when running with:

   for(int i = 0; i < N; i++){
      array[i] = new HashSet<Integer>();
   }

All is well.

Why does the first code isn't working? Is it my mistake?

Thank you

Upvotes: 0

Views: 4205

Answers (5)

Prudhvi Rao
Prudhvi Rao

Reputation: 1

You need to create an array that contains HashSet so, you need to initialize the array and then initialize the HashSet which I have provided in the below code

 int N = 100;
Set<Integer>[] array = new Set[n];

    for(int i = 0; i < n; i++) {
        array[i] = new HashSet<Integer>();
    }

Upvotes: 0

spats
spats

Reputation: 853

Simply use new Set[n] instead of new HashSet[n] , to get it working

Set<Integer>[] list = new Set[n];
for(int i=0;i<n;i++)
  list[i] = new HashSet<>();

Upvotes: 0

Cheung
Cheung

Reputation: 174

two 'for' is different in Java, look the java code & bytecode .. Example:

public class T{
        public static void main(String[] args){
            String[] data = new String[10];
            System.out.print("");
            for(String str:data){
                str="1";
            }   
            System.out.print("");
            for(int i=0;i<data.length;i++){
                data[i]="1";
            }
        }
    }

$>javac -classpath . T.java
$>javap -c T


Compiled from "T.java"
public class T extends java.lang.Object{
public T();
  Code:
   0:   aload_0
   1:   invokespecial   #1; //Method java/lang/Object."<init>":()V
   4:   return

public static void main(java.lang.String[]);
  Code:
   0:   bipush  10
   2:   anewarray   #2; //class java/lang/String
   5:   astore_1
   6:   getstatic   #3; //Field java/lang/System.out:Ljava/io/PrintStream;
   9:   ldc #4; //String 
   11:  invokevirtual   #5; //Method java/io/PrintStream.print:(Ljava/lang/String;)V
   14:  aload_1
   15:  astore_2
   16:  aload_2
   17:  arraylength
   18:  istore_3
   19:  iconst_0
   20:  istore  4
   22:  iload   4
   24:  iload_3
   25:  if_icmpge   44
   28:  aload_2
   29:  iload   4
   31:  aaload
   32:  astore  5
   34:  ldc #6; //String 1
   36:  astore  5
   38:  iinc    4, 1
   41:  goto    22
   44:  getstatic   #3; //Field java/lang/System.out:Ljava/io/PrintStream;
   47:  ldc #4; //String 
   49:  invokevirtual   #5; //Method java/io/PrintStream.print:(Ljava/lang/String;)V
   52:  iconst_0
   53:  istore_2
   54:  iload_2
   55:  aload_1
   56:  arraylength
   57:  if_icmpge   71
   60:  aload_1
   61:  iload_2
   62:  ldc #6; //String 1
   64:  aastore
   65:  iinc    2, 1
   68:  goto    54
   71:  return

}

from line 25--44 and line 57--71: aload : Retrieves an object reference from a local variable and pushes it onto the operand stack. aaload : Retrieves an object reference from an array of objects and places it on the stack.

astore : Take object or reference store to local variable. aastore : Take reference type value store to array.

so,first can't store array , didn't using initializing array.

Upvotes: 1

Mordechai
Mordechai

Reputation: 16284

An enhanced for loop, does not use the actual instances in the array( and as well, collection), but rather copies them in the loop control variable.

This should not be a problem with non-null values, since they point to the same object. Problems arise, if the values are null, and re-assigning values to the control variable, would not change the actual value. So in this case, always use regular for loops.

The same would be true, to arrays of primitive types, since coping them, and changing the copied variable, will not affect the original variable.

Upvotes: 0

Perception
Perception

Reputation: 80623

You never actually assign initialized instances to elements of the array. Instead you iterate over the elements of the array with a variable that gets assigned to a new object in your loop, then is never used. In a case like this the enhanced for...each syntax is not appropriate, use the traditional for loop instead.

Upvotes: 3

Related Questions