Solved

Core Java. One dimensional array. Swap Function. Usage of Null. Everything work just question.

Posted on 2016-07-27
6
71 Views
Last Modified: 2016-08-06
I have a case, I need to rewrite data from one array to another at the way that all integer below zero were at first places.

Input : OriginalVector[] = {1, 2, 3, 4,-9, 54, 5, -23} ;
           NewVector[] =       {0, 0 , 0 , 0 , 0 , 0 , 0, 0} ;
OutPut : OriginalVector[] = {1, 2, 3, 4,-9, 54, 5, -23} ;
              NewVector[] =       {-9, -23 , 1 , 2 , 3 , 4 , 54, 5} ;

So the code below works fine, however, I've few questions, please help me to figure out.
1. Why I cannot give "OriginalVector[]" array variables SIZE to finalize it size. (   OriginalVector[SIZE] ) ?
2. Why I cannot make NewVector[] = null ; - eclipse gives me compile error
Exception in thread "main" java.lang.NullPointerException
      at TaskCh.TaskCh.main(TaskCh011N245.java:26)

3. Are the any standard implementation of swap function is java ,  like std:swap in Cpp ?
Please take a look on the code below ! Looking forward for reply from you dear Experts ! thank you in advance !


package TaskCh;



public class TaskCh {


	@SuppressWarnings("null")
	public static void main(String[] args) {
		// TODO Auto-generated method stub

		
		final int SIZE = 8 ; 
		
		int OriginalVector[] = {1, 2, 3, 4,-9, 54, 5, -23} ;
	 	int NewVector[] = {0, 0 , 0 , 0 , 0 , 0 , 0, 0} ;
	//	int NewVector[] = null ; 
		int i, j , k;
		
		for ( i = 0 , j = 0 , k = SIZE -1 ; i < SIZE ; ++i){
			
			if ( OriginalVector[i] < 0){
				NewVector[j++] = OriginalVector[i];
			}
			else {
				NewVector[k--] = OriginalVector[i];
			}
		}
		
		for ( i = k + 1 , j = SIZE - 1 ; i < j ; ++i, --j){
			
			NewVector[i] = NewVector[i] +  NewVector[j];
			NewVector[j] = NewVector[i] - NewVector[j]; 
			NewVector[i] = NewVector[i] - NewVector[j]; 
		}
		
		for ( int m = 0 ; m < SIZE ; m ++ ){
			System.out.println(NewVector[m]); 
		}
		
		
	}

}

Open in new window

0
Comment
Question by:SunnyX
  • 3
6 Comments
 
LVL 35

Accepted Solution

by:
mccarl earned 370 total points
ID: 41732200
1. Why I cannot give "OriginalVector[]" array variables SIZE to finalize it size. (   OriginalVector[SIZE] ) ?

You can't because you have already basically defined the number of elements by including 8 values in the literal array that follows it. So for example, what you could do for NewVector is just this...

int NewVector[SIZE];

or even this...

int NewVector[OriginalVector.length];


2. Why I cannot make NewVector[] = null

Because, null means no array at all. This is different to an empty array (which is what you have defined or what I have shown just above). And because when you use null, the array doesn't even exist yet, the NullPointerException is the result of you trying to put a value into something that doesn't exist.


3. Are the any standard implementation of swap function is java

Not that I know of. However, i would got about this problem a bit different where you would need to swap anything. If you want to try and think of another way, think about doing 2 iterations of the original vector... the first looks for negative numbers and fills the new vector as it finds them. Then the second pass looks for positive numbers and keeps filling the new vector.

(Yes, I being a little bit vague because I don't want to just give you the full solution but rather get you thinking about it. But definitely ask questions if you don't get my idea)
1
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 130 total points
ID: 41732550
1
 

Author Comment

by:SunnyX
ID: 41732869
I confirm that all my questions aren't academic ! I've experience to work with knowledge exchange communities and I know that if I start to ask question providing, for example,  real ( full long ) code, the speed of answer drops dramatically ! Therefore ( and this is obvious ) It's more effective to parse the question to the simplest form of smaller once  and remains only to core of the problem.
0
 

Author Closing Comment

by:SunnyX
ID: 41732872
Thx you dear experts for your help !
0
 

Author Comment

by:SunnyX
ID: 41732906
mccarl , sorry but

int NewVector[OriginalVector.length];

Open in new window

isn't work compile error please take a look on print screen

https://gyazo.com/0fd86638fc2c118bc036cb7fdf1bf8f9

I tried modification static / final still compile error
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
java.io.BufferedReader   readLine()  //returns the entire file ? 4 60
Java Timer (static) 9 36
custom annotations 9 33
difference of if loops 23 43
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
I had a project requirement for a displaying a user workbench .This workbench would consist multiple data grids .In each grid the user will be able to see a large number of data. These data grids should allow the user to 1. Sort 2. Export the …
The viewer will learn how to implement Singleton Design Pattern in Java.
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

23 Experts available now in Live!

Get 1:1 Help Now