13 Replies - 924 Views - Last Post: 03 August 2010 - 03:00 PM Rate Topic: -----

#1 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

how to use vectors.

Posted 02 August 2010 - 09:24 PM

I'm jumping into java a little bit just cuz, I'm mostly a c++ kinda guy. i want to make a sieve of Eratosthenes program but I'm having trouble with vectors. i only have a little bit of the program down right now but i have no clue how to use the vectors correctly. here is what little code i have so far.

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helloworld;
import java.util.Vector;
/**
 *
 * @author ishka
 */
public class Main {
    public void SeviePrimteUpTo(long upto) {
        long p=2,i,k,x=0;
        Vector<boolean> TheList = new Vector<boolean>(); ///gives me error right here
    }

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }

}




Is This A Good Question/Topic? 0
  • +

Replies To: how to use vectors.

#2 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10665
  • View blog
  • Posts: 39,608
  • Joined: 27-December 08

Re: how to use vectors.

Posted 02 August 2010 - 09:26 PM

You can't use primitives with Generics. You'll have to use the Boolean wrapper class instead.

Also, try using ArrayList instead of Vector. They have the same methods, but Vector is less efficient than ArrayList b/c it is Thread-safe.
Was This Post Helpful? 2
  • +
  • -

#3 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: how to use vectors.

Posted 02 August 2010 - 09:44 PM

ok that's very different from c++ i assume that java standards provide a full set of wrappers for just this reason, if so are they all just first letter upper case versions of there base counter parts? also i don't see any operator overloads, dose java have them? if so why are they not utilized in ArrayList
Was This Post Helpful? 0
  • +
  • -

#4 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10665
  • View blog
  • Posts: 39,608
  • Joined: 27-December 08

Re: how to use vectors.

Posted 02 August 2010 - 09:47 PM

With the exception of char and int, which have the Wrapper classes Character and Integer respectively, yes. Java does not support operator overloading. It is a different animal than C++, designed to be more abstract and high-level.
Was This Post Helpful? 1
  • +
  • -

#5 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: how to use vectors.

Posted 02 August 2010 - 09:59 PM

ok, now i'm having trouble with the math library this one seemed straight forward but apparently it's not. it says it cant find the symbol "sqrt" but i see it listed as a function of java.lang.Math class. here is my code so you can see what it is I'm doing wrong.
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helloworld;
import java.util.ArrayList;
import java.lang.Math;
/**
 *
 * @author ishka
 */
public class Main {
    public void SeviePrimteUpTo(int upto) {
        int p=2,i,k,x=0;
        ArrayList<Boolean> TheList = new ArrayList<Boolean>(upto);
        for(i=0;i<upto;++i) TheList.set(i, Boolean.TRUE);
        k=upto+1;
        upto = sqrt((double)upto);

    }

    public static void main(String[] args) {
        System.out.println("Hello World!");
    }

}




o and just so you can see where i am translating this code from here is my c++ version that's been completed.

void SeviePrimesUpTo(uint32_t upto) {
    uint32_t p=2,i,k,x=0;
    vector<bool> TheList(upto+1, true);
    k=upto+1;
    upto=sqrt(double(upto));
    while (p<=upto) {
        if (TheList[p]==true) {
            for (i=p*p; i < k;i+=p) {
                TheList[i] = 0;
            }
        }
        ++p;
    }
    for (i=2;i<k;++i) {
        if ( TheList[i]) {
            //cout<<i<<"\n";
            printf("%i\n",i);
        }
    }
}


Was This Post Helpful? 0
  • +
  • -

#6 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10665
  • View blog
  • Posts: 39,608
  • Joined: 27-December 08

Re: how to use vectors.

Posted 02 August 2010 - 10:01 PM

You have to invoke Math.sqrt(), as invoking sqrt() implies that sqrt() is defined in the class you are working in. In Java, the java.lang package is auto-imported, but you have to reference elements outside of the class you are working in using the class name if they are static, and Object name if they are non-static.
Was This Post Helpful? 1
  • +
  • -

#7 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: how to use vectors.

Posted 02 August 2010 - 10:07 PM

ok, cool deal that fixed everything for now thanks a ton;
Was This Post Helpful? 0
  • +
  • -

#8 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10665
  • View blog
  • Posts: 39,608
  • Joined: 27-December 08

Re: how to use vectors.

Posted 02 August 2010 - 10:10 PM

Glad I could help! :)
Was This Post Helpful? 0
  • +
  • -

#9 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: how to use vectors.

Posted 02 August 2010 - 10:26 PM

alright i have one last question, i get no errors dynamically as they have been caught every other time but now it's throwing exceptions 4 places in my code but 1) i dont know what error is causing the exception to be thrown 2)i don't even know specifically what the error is. these things make it very hard to fix my code.
here are the messages i get.

Quote

run:
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 0, Size: 0
at java.util.ArrayList.RangeCheck(ArrayList.java:547)
at java.util.ArrayList.set(ArrayList.java:337)
at helloworld.Main.SeviePrimteUpTo(Main.java:16)
at helloworld.Main.main(Main.java:35)
Java Result: 1
BUILD SUCCESSFUL (total time: 0 seconds)


and here is my code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helloworld;
import java.util.ArrayList;
/**
 *
 * @author ishka
 */
public class Main {
    public static void SeviePrimteUpTo(int upto) {
        int p=2,i,k,x=0;
        ArrayList<Boolean> TheList = new ArrayList<Boolean>(upto);
        for(i=0;i<upto;++i) TheList.set(i, true);
        k=upto+1;
        upto = (int)Math.sqrt((double)upto);
        while(p<=upto) {
            if(TheList.get(p)) {
                for(i=p*p;i<k;i+=p) {
                    TheList.set(i, false);
                }
            }
            ++p;
        }
        for(i=2;i<k;++i) {
            if(TheList.get(i)) {
                System.out.println(i);
            }
        }
    }

    public static void main(String[] args) {
        SeviePrimteUpTo(49);
    }

}



Was This Post Helpful? 0
  • +
  • -

#10 macosxnerd101  Icon User is online

  • Self-Trained Economist
  • member icon




Reputation: 10665
  • View blog
  • Posts: 39,608
  • Joined: 27-December 08

Re: how to use vectors.

Posted 02 August 2010 - 10:40 PM

With Lists in Java, capacity != size. So just b/c you declare upto elements, doesn't mean you have upto spots being made available to you the developer (though the array in the background has that capacity). Rather than using set(), better just to use add(E elem) in this case.
Was This Post Helpful? 0
  • +
  • -

#11 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: how to use vectors.

Posted 02 August 2010 - 10:41 PM

actually under closer inspection i figured out that only one thing was being thrown and that was because i was making a very dumb mistake of trying access an indexed location that had not been allocated, thanks again.

here is the finished code:
/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package helloworld;
import java.util.ArrayList;
/**
 *
 * @author ishka
 */
public class Main {
    public static void SeviePrimteUpTo(int upto) {
        int p=2,i,k,x=0;
        ArrayList<Boolean> TheList = new ArrayList<Boolean>(upto+1);
        for(i=0;i<upto+1;++i) TheList.add(true);
        k=upto+1;
        upto = (int)Math.sqrt((double)upto);
        while(p<=upto) {
            if(TheList.get(p)) {
                for(i=p*p;i<k;i+=p) {
                    TheList.set(i, false);
                }
            }
            ++p;
        }
        for(i=2;i<k;++i) {
            if(TheList.get(i)) {
                System.out.println(i);
            }
        }
    }

    public static void main(String[] args) {
        SeviePrimteUpTo(49);
    }

}



Was This Post Helpful? 0
  • +
  • -

#12 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: how to use vectors.

Posted 02 August 2010 - 10:50 PM

Your arraylist is empty. You can't set a non-existent element. Use add() to populate it instead:

Change

...
ArrayList<Boolean> TheList = new ArrayList<Boolean>(upto);
for (i = 0; i < upto; ++i) {
    TheList.set(i, true);
}
...



to

...
ArrayList<Boolean> TheList = new ArrayList<Boolean>(upto);
for (i = 0; i < upto; ++i) {
    TheList.add(true);
}
...



See Class ArrayList<E>

This post has been edited by n8wxs: 02 August 2010 - 10:50 PM

Was This Post Helpful? 0
  • +
  • -

#13 ishkabible  Icon User is offline

  • spelling expret
  • member icon




Reputation: 1622
  • View blog
  • Posts: 5,709
  • Joined: 03-August 09

Re: how to use vectors.

Posted 02 August 2010 - 11:07 PM

lol i figured that, thanks anyway however.
Was This Post Helpful? 0
  • +
  • -

#14 pbl  Icon User is offline

  • There is nothing you can't do with a JTable
  • member icon

Reputation: 8342
  • View blog
  • Posts: 31,880
  • Joined: 06-March 08

Re: how to use vectors.

Posted 03 August 2010 - 03:00 PM

For your Sieve better to use a BitSet rather than an ArrayList and/or Vector
It will take 8 times less memory and will avoid to create a Boolean object for each bit you will set on

Later on when you will have exhausted your computer memory you can use my CodeSnippet that makes a BitSet on a RandomAccess file with a nice GUI to see how your computations are going :)

http://www.dreaminco...snippet4665.htm
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1