5 Replies - 264 Views - Last Post: 12 January 2013 - 07:34 PM Rate Topic: -----

#1 Pin Head  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 236
  • Joined: 31-July 09

Problem ArrayList< float[ ] > inputting new numbers

Posted 12 January 2013 - 06:52 PM

Hi,
I'm a little baffled by this problem okay my float array contains the right float numbers but when I passed them into
my ArrayList that contains a float array the array seems to have new numbers inserted into it

public void test1()
	{
		
		ArrayList<String >sNumbers = new ArrayList< String >();
		sNumbers.add( "1.0" );
		sNumbers.add( "1.0" );
		sNumbers.add( "-1.0" );
		sNumbers.add( "-1.0");
		sNumbers.add( "1.0");
		sNumbers.add( "0.999999");
		sNumbers.add( "-1.0" );
		sNumbers.add( "-1.0" );
					
		float[]test = new float[ sNumbers.size() ];
		
		ArrayList< float[] > list = new ArrayList< float[] >(); 
		
		for( int u = 0; u < test.length; u+=2 )
		{
			test[ u ] = Float.parseFloat( sNumbers.get( u ) );
			System.out.println( test[ u ] );
		}
		
		list.add( test );
		
		for( int i = 0; i < list.size(); i++ )
		{
			System.out.println(  i );
			System.out.println( "Verts: " + Arrays.toString( list.get( i ) ) );
							
		}
		for( int y = 0; y < test.length; y++ )
		{
			System.out.println( "Verts: " + list.get( 0 )[ y ] );				
		}

	}




this prints

1.0
-1.0
1.0
-1.0
0
Verts: [1.0, 0.0, -1.0, 0.0, 1.0, 0.0, -1.0, 0.0]
Verts: 1.0
Verts: 0.0
Verts: -1.0
Verts: 0.0
Verts: 1.0
Verts: 0.0

Is This A Good Question/Topic? 0
  • +

Replies To: Problem ArrayList< float[ ] > inputting new numbers

#2 FallenG  Icon User is offline

  • New D.I.C Head

Reputation: 22
  • View blog
  • Posts: 44
  • Joined: 12-January 13

Re: Problem ArrayList< float[ ] > inputting new numbers

Posted 12 January 2013 - 07:11 PM

Well you only filled half the values in test, so the others are initialised to 0.0 by default...
Was This Post Helpful? 1
  • +
  • -

#3 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7950
  • View blog
  • Posts: 13,543
  • Joined: 19-March 11

Re: Problem ArrayList< float[ ] > inputting new numbers

Posted 12 January 2013 - 07:14 PM

This may be a stupid question, but what are you trying to do here?

I can't make head or tail of it - what's the desired result that you're not getting?
Was This Post Helpful? 0
  • +
  • -

#4 Pin Head  Icon User is offline

  • D.I.C Head
  • member icon

Reputation: 7
  • View blog
  • Posts: 236
  • Joined: 31-July 09

Re: Problem ArrayList< float[ ] > inputting new numbers

Posted 12 January 2013 - 07:22 PM

Hi,
I don't want these the numbers 0.0 in the float array

this is the result I'm getting
Verts: [1.0, 0.0, -1.0, 0.0, 1.0, 0.0, -1.0, 0.0]

but I was expecting this result
Verts: [1.0,-1.0,1.0,-1.0 ]
Was This Post Helpful? 0
  • +
  • -

#5 jon.kiparsky  Icon User is offline

  • Pancakes!
  • member icon


Reputation: 7950
  • View blog
  • Posts: 13,543
  • Joined: 19-March 11

Re: Problem ArrayList< float[ ] > inputting new numbers

Posted 12 January 2013 - 07:28 PM

In that case, FallenG has put his finger on it:
for( int u = 0; u < test.length; u+=2 )


Because you increment by twos, you're skipping over the odd-numbered indexes, which remain initialized to zero.
Was This Post Helpful? 0
  • +
  • -

#6 FallenG  Icon User is offline

  • New D.I.C Head

Reputation: 22
  • View blog
  • Posts: 44
  • Joined: 12-January 13

Re: Problem ArrayList< float[ ] > inputting new numbers

Posted 12 January 2013 - 07:34 PM

View PostPin Head, on 12 January 2013 - 07:22 PM, said:

Hi,
I don't want these the numbers 0.0 in the float array

this is the result I'm getting
Verts: [1.0, 0.0, -1.0, 0.0, 1.0, 0.0, -1.0, 0.0]

but I was expecting this result
Verts: [1.0,-1.0,1.0,-1.0 ]

As I said, you created an array with a size twice as large as the number of elements you are filling it with. All values in a [in]Float[][/in] are initialised to [in]0.0[/in] be default.

Perhaps you meant something like this?
		float[] test = new float[sNumbers.size() / 2];
		ArrayList< float[] > list = new ArrayList< float[] >(); 
		
		for( int u = 0; u < sNumbers.size(); u+=2 )
		{
			test[ u/2 ] = Float.parseFloat( sNumbers.get( u ) );
		}

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1