Results 1 to 12 of 12

Thread: Java C#

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Registered User bombov's Avatar
    Join Date: Jan:2007
    Location:
    Posts: 260

    Java C#

    , . Java , , C#.

    E :

    Code:
    import java.util.*;
    import java.util.List;
    import java.util.ArrayList;
    
    public class list1 {
    
    	public static int calcSum(List<Integer> list) {
    		
    		int sum=0;
    		for (int num :list){
    			
    			sum+=num;
    		}
    		return sum;
    		
    	}
    	public static void PrintAllElements(List<Integer> list) {
    		
    		int print=0;
    		while(!list.isEmpty()){
    			
    			print = list.remove(0);
    			System.out.print(print+" ");
    		}
    	}
    	
    	public static void main(String[] args) {
    		Scanner s = new Scanner(System.in);
    		
    		List<Integer> numbers = new ArrayList<Integer>();
    		int counter=0;
    		while (true) {
    			System.out.println("Enter number:");
    			int num=  s.nextInt();
    			counter++;
    			if (num > 0)
    			numbers.add(num);
    			
    			else if (num ==0)
    				break;
    		}
    		
    		int sum = calcSum(numbers);
    		System.out.println("Sum = "+sum);
    		//System.out.println(counter);
    		for(int i=1;i<=counter;i++) {
    		PrintAllElements(numbers);
    		}
    	}
    }
    - :

    for (int num :list)
    , : foreach (int num in list)

    Code:
    List<Integer> numbers = new ArrayList<Integer>();
    Code:
    while(!list.isEmpty())
    Code:
    print = list.remove(0);
    .
    Last edited by bombov; 17th March 2009 at 22:40.

  2. #2
    rogozen's Avatar
    Join Date: Nov:2005
    Location:
    Posts: 357
    ,
    Quote Originally Posted by bombov View Post
    Code:
    List<Integer> numbers = new ArrayList<Integer>();
    Code:
    while(!list.isEmpty())
    Code:
    print = list.remove(0);
    :
    Code:
    List<int> numbers = new List<int>();
    Code:
    while(list.Count > 0)
    Code:
    print = list[0];
    list.RemoveAt(0);
    PS. List<T> http://msdn.microsoft.com/en-us/library/6sh2ey19.aspx

  3. #3
    Registered User bombov's Avatar
    Join Date: Jan:2007
    Location:
    Posts: 260
    Code:
    public static void PrintAllElements(List<Int32> list) {
    		
    		int print=0;
    		while(list.Count > 0){
    
              			print = list[0];
    			list.RemoveAt(0);
    			Console.WriteLine(print+" ");
               			
    		    }
    	    }
    :
    Code:
        //counter     
                for (int i = 1; i <= counter; i++)
                {
                    PrintAllElements(numbers, counter);
                }
    , ?

  4. #4
    Bombera's Avatar
    Join Date: Jul:2001
    Location: 4EVA
    Posts: 13,833
    Quote Originally Posted by bombov View Post
    Code:
    public static void PrintAllElements(List<Int32> list) {
    		
    		int print=0;
    		while(list.Count > 0){
    
              		print = list[0];
    			list.RemoveAt(0);
    			Console.WriteLine(print+" ");
               			
    		    }
    	    }
    , ?
    . , print , , . , -, , . -
    EVGA X299 FTW K|i9-7960X@4.7|4x8 Patriot Viper Steel 4000|GTX 1660 Ti|970 EVO 1 TB|Seasonic Focus GX-1000|Xigmatek Elysium|
    Rampage IV Extreme BE|E5-1680v2@4.7|4x4 HyperX 1866|Cougar Aqua 240|GTX 1050 Ti|970 EVO 1/4 TB|CM 850 SilentPro|HAF-X|

  5. #5
    ɐ-əpoɔᴉu⋂ ɐ ə anrieff's Avatar
    Join Date: Apr:2004
    Location: Sofia
    Posts: 8,448
    , , 1 , ?
    , . .
    "640K ught to be enough for anybody" - Bill Gates, 1981
    ::Machine specs::Fract::AGG::::Baileys::blog::YouTube channel

  6. #6
    Registered User bombov's Avatar
    Join Date: Jan:2007
    Location:
    Posts: 260
    Quote Originally Posted by anrieff View Post
    , , 1 , ?
    . 1 .

    @Bombera

    :

    Code:
     Console.WriteLine(list[0] + " ");
                list.RemoveAt(0);
    :

    Code:
    // :
    
     Console.Write("Elements are - ");
                PrintAllElements(numbers);
    Code:
    // :
    public static void PrintAllElements(List<Int32> list) {
            foreach (int num in list)
                {
                Console.Write(num + " ");
                }
    	    }

  7. #7
    Registered User 's Avatar
    Join Date: Feb:2006
    Location:
    Posts: 653
    Quote Originally Posted by bombov View Post
    ...
    Code:
    // :
    public static void PrintAllElements(List<Int32> list) {
            foreach (int num in list)
                {
                Console.Write(num + " ");
                }
    	    }
    :
    Code:
    public static void PrintAllElements(List<Int32> list) {
            for (int i = 0; i < list.Count; i++)
                {
                Console.Write(string.Format("{0} ", list[i]));
                }
    	    }

  8. #8
    Bombera's Avatar
    Join Date: Jul:2001
    Location: 4EVA
    Posts: 13,833
    , , , . , .
    EVGA X299 FTW K|i9-7960X@4.7|4x8 Patriot Viper Steel 4000|GTX 1660 Ti|970 EVO 1 TB|Seasonic Focus GX-1000|Xigmatek Elysium|
    Rampage IV Extreme BE|E5-1680v2@4.7|4x4 HyperX 1866|Cougar Aqua 240|GTX 1050 Ti|970 EVO 1/4 TB|CM 850 SilentPro|HAF-X|

  9. #9
    Bombera's Avatar
    Join Date: Jul:2001
    Location: 4EVA
    Posts: 13,833
    , ?
    EVGA X299 FTW K|i9-7960X@4.7|4x8 Patriot Viper Steel 4000|GTX 1660 Ti|970 EVO 1 TB|Seasonic Focus GX-1000|Xigmatek Elysium|
    Rampage IV Extreme BE|E5-1680v2@4.7|4x4 HyperX 1866|Cougar Aqua 240|GTX 1050 Ti|970 EVO 1/4 TB|CM 850 SilentPro|HAF-X|

  10. #10
    Registered User 's Avatar
    Join Date: Feb:2006
    Location:
    Posts: 653
    Quote Originally Posted by Bombera View Post
    , ?
    Consider for Instead of foreach

    Use for instead of foreach (C#) to iterate the contents of arrays or collections in performance critical code, particularly if you do not need the protections offered by foreach.

    Both foreach in C# and For Each in Visual Basic .NET use an enumerator to provide enhanced navigation through arrays and collections. As discussed earlier, typical implementations of enumerators, such as those provided by the .NET Framework, will have managed heap and virtual function overhead associated with their use.

    If you can use the for statement to iterate over your collection, consider doing so in performance sensitive code to avoid that overhead.
    .

  11. #11
    Bombera's Avatar
    Join Date: Jul:2001
    Location: 4EVA
    Posts: 13,833
    . .
    , , , Console
    , foreach , ? ? .
    EVGA X299 FTW K|i9-7960X@4.7|4x8 Patriot Viper Steel 4000|GTX 1660 Ti|970 EVO 1 TB|Seasonic Focus GX-1000|Xigmatek Elysium|
    Rampage IV Extreme BE|E5-1680v2@4.7|4x4 HyperX 1866|Cougar Aqua 240|GTX 1050 Ti|970 EVO 1/4 TB|CM 850 SilentPro|HAF-X|

  12. #12
    rogozen's Avatar
    Join Date: Nov:2005
    Location:
    Posts: 357
    foreach Exception. List<T>.Enumerator , foreach List<T>, _version. , .

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

Copyright © 1999-2011 . .
iskamPC.com | mobility.BG | Bloody's Techblog | | 3D Vision Blog |